RainX_69

Shortest Path Using Atmost One Curved Edge | ASKED IN AN INTERVIEW

Feb 25th, 2023 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | Source Code | 0 0
  1. Link- https://practice.geeksforgeeks.org/problems/e7d81a082cda6bd1e959d943197aa3bc21b88bdb/1
  2.  
  3. Given an undirected connected graph of n vertices and list of m edges in a graph and for each pair of vertices that are connected by an edge.
  4.  
  5. There are two edges between them, one curved edge and one straight edge i.e. the tuple (x, y, w1, w2) means that between vertices x and y, there is a straight edge with weight w1 and a curved edge with weight w2.
  6.  
  7. You are given two vertices a and b and you have to go from a to b through a series of edges such that in the entire path you can use at most 1 curved edge. Your task is to find the shortest path from a to b satisfying the above condition. If there is no path from a to b, return -1.
  8.  
  9.  
  10.  
  11. Example 1:
  12.  
  13. Input:
  14. n = 4, m = 4
  15. a = 2, b = 4
  16. edges = {{1, 2, 1, 4}, {1, 3, 2, 4},
  17.          {1, 4, 3, 1}, {2, 4, 6, 5}}
  18. Output:
  19. 2
  20.  
  21. Explanation:
  22. We can follow the path 2 -> 1 -> 4.
  23. This gives a distance of 1+3 = 4 if we follow
  24. all straight paths. But we can take the curved
  25. path  from 1 -> 4, which costs 1. This
  26. will result in a cost of 1+1 = 2
  27.  
  28. Example 2:
  29. Input:
  30. n = 2, m = 1
  31. a = 1, b = 2
  32. edges = {{1, 2, 4, 1}}
  33. Output :
  34. 1
  35.  
  36. Explanation:
  37. Take the curved path from 1 to 2 which costs 1.
  38.  
  39. Your Task:  
  40. You don't need to read input or print anything. Your task is to complete the function shortestPath() which takes 4 integers n, m, a, and b, and a list of lists named edges of size m as input and returns the cost of shortest path from a to b.
  41.  
  42.  
  43. Expected Time Complexity: O((m+n)log(n))
  44. Expected Auxiliary Space: O(n+m)
  45.  
  46.  
  47. Constraints:
  48. 1 ≤ n,m ≤ 10^5
  49. 1 ≤ a,b ≤ n
  50. weight of edges ≤ 10^4       
  51.  
  52. // ------------------------------------------------------------------------------------------------------------------------------------
  53.     struct info{
  54.         int node;
  55.             int wt;
  56.             int curve_wt;
  57.         };
  58.  
  59.  
  60.    list<info>* adj;
  61.  
  62.    int shortestPath(int n, int m, int source, int dest, vector<vector<int>> &edges) {
  63.        adj=new list<info>[n+1];
  64.        for(auto edge: edges){
  65.            adj[edge[0]].push_back({edge[1],edge[2],edge[3]});
  66.            adj[edge[1]].push_back({edge[0],edge[2],edge[3]});
  67.        }
  68.        vector<int> StraightDist(n+1,INT_MAX);
  69.        vector<int> CurvedDist(n+1,INT_MAX);
  70.        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
  71.        
  72.        pq.push({0,source});
  73.        StraightDist[source]=0;
  74.        CurvedDist[source]=0;
  75.        
  76.        while(!pq.empty()){
  77.            auto curr=pq.top();
  78.            pq.pop();
  79.            
  80.            int srcNode=curr.second;
  81.            int dist=curr.first;
  82.            
  83.            for(auto nei: adj[srcNode]){
  84.                if(StraightDist[srcNode] + nei.curve_wt < CurvedDist[nei.node]){  
  85.                     // walking straight till srcNode and then using a curved edge
  86.                    CurvedDist[nei.node]=StraightDist[srcNode] + nei.curve_wt;
  87.                    pq.push({CurvedDist[nei.node],nei.node});
  88.                }
  89.                
  90.                // below are just generic dijsktra logic
  91.                
  92.                if(CurvedDist[srcNode] + nei.wt < CurvedDist[nei.node]){
  93.                /* this says we had used some curved edge to reach srcNode from source(using 1 curve),
  94.                   check if using a straigt path from srcNode to nei.node is a wise one. Or, the path
  95.                   from src to nei.node using a curved path is already less */
  96.                    CurvedDist[nei.node]=CurvedDist[srcNode] + nei.wt;
  97.                    pq.push({CurvedDist[nei.node],nei.node});
  98.                }
  99.  
  100.                if(StraightDist[srcNode] + nei.wt < StraightDist[nei.node]){
  101.                  /* this says we had used only straight edge to reach srcNode from source,
  102.                   check if using a straigt path from srcNode to nei.node is a wise one. Or, the path
  103.                   from src to nei.node using a straight path is already less */
  104.                    StraightDist[nei.node]=StraightDist[srcNode] + nei.wt;
  105.                    pq.push({StraightDist[nei.node],nei.node});
  106.                }
  107.            }
  108.        }
  109.        int res=min(CurvedDist[dest],StraightDist[dest]); // checking for both straight and curved
  110.        return res==INT_MAX ? -1 : res;
  111.    }
  112.  
  113.  
  114.  
  115.  
  116.  
Advertisement
Add Comment
Please, Sign In to add comment