RainX_69

Minimum Cost of a Path With Special Roads | MUST DO | TRICKY | OA

May 2nd, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | Source Code | 0 0
  1. https://leetcode.com/problems/minimum-cost-of-a-path-with-special-roads/
  2.  
  3. You are given an array start where start = [startX, startY] represents your initial position (startX, startY) in a 2D space. You are also given the array target where target = [targetX, targetY] represents your target position (targetX, targetY).
  4. The cost of going from a position (x1, y1) to any other position in the space (x2, y2) is |x2 - x1| + |y2 - y1|.
  5. There are also some special roads. You are given a 2D array specialRoads where specialRoads[i] = [x1i, y1i, x2i, y2i, costi] indicates that the ith special road can take you from (x1i, y1i) to (x2i, y2i) with a cost equal to costi. You can use each special road any number of times.
  6.  
  7. Return the minimum cost required to go from (startX, startY) to (targetX, targetY).
  8.  
  9. Example 1:
  10. Input: start = [1,1], target = [4,5], specialRoads = [[1,2,3,3,2],[3,4,4,5,1]]
  11. Output: 5
  12. Explanation: The optimal path from (1,1) to (4,5) is the following:
  13. - (1,1) -> (1,2). This move has a cost of |1 - 1| + |2 - 1| = 1.
  14. - (1,2) -> (3,3). This move uses the first special edge, the cost is 2.
  15. - (3,3) -> (3,4). This move has a cost of |3 - 3| + |4 - 3| = 1.
  16. - (3,4) -> (4,5). This move uses the second special edge, the cost is 1.
  17. So the total cost is 1 + 2 + 1 + 1 = 5.
  18. It can be shown that we cannot achieve a smaller total cost than 5.
  19.  
  20. Example 2:
  21. Input: start = [3,2], target = [5,7], specialRoads = [[3,2,3,4,4],[3,3,5,5,5],[3,4,5,6,6]]
  22. Output: 7
  23. Explanation: It is optimal to not use any special edges and go directly from the starting to the ending position with a cost |5 - 3| + |7 - 2| = 7.
  24.  
  25.  
  26. Constraints:
  27.  
  28. start.length == target.length == 2
  29. 1 <= startX <= targetX <= 10^5
  30. 1 <= startY <= targetY <= 10^5
  31. 1 <= specialRoads.length <= 200
  32. specialRoads[i].length == 5
  33. startX <= x1i, x2i <= targetX
  34. startY <= y1i, y2i <= targetY
  35. 1 <= costi <= 10^5
  36.  
  37. ---------------------------------------------------------------------------------------------------------------------------------------
  38.  
  39. class Solution {
  40. public:
  41.     int minimumCost(vector<int>& start, vector<int>& target, vector<vector<int>>& specialRoads) {
  42.         queue<pair<int,pair<int,int>>> q;
  43.  
  44.         int n=specialRoads.size();
  45.         vector<int> dp(n+1,INT_MAX);
  46.        
  47.         q.push({0,{start[0],start[1]}});
  48.        
  49.         int res=INT_MAX;
  50.        
  51.         while(!q.empty()){
  52.             auto info=q.front();
  53.             q.pop();
  54.            
  55.             int dist=info.first;
  56.             int x=info.second.first;
  57.             int y=info.second.second;
  58.            
  59.             res=min(res,dist+abs(target[0]-x)+abs(target[1]-y));
  60.            
  61.             int i=0;
  62.             for(auto c: specialRoads){
  63.                 int sx=c[0];
  64.                 int sy=c[1];
  65.                 int dx=c[2];
  66.                 int dy=c[3];
  67.                 int cost=c[4];
  68.                 if(abs(x-sx)+abs(y-sy)+dist+cost<dp[i]){
  69.                     q.push({abs(x-sx)+abs(y-sy)+dist+cost,{dx,dy}});
  70.                     dp[i]=abs(x-sx)+abs(y-sy)+dist+cost;
  71.                 }
  72.                 i++;
  73.             }
  74.         }
  75.         return res;
  76.     }
  77. };
Advertisement
Add Comment
Please, Sign In to add comment