RainX_69

Minimum cost to reach destination from source using two modes of transport | MUST DO | OA

May 15th, 2023
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.37 KB | Source Code | 0 0
  1. https://practice.geeksforgeeks.org/contest/gfg-weekly-coding-contest-102/problems/
  2.  
  3. There are N cities, from 0 to N-1. Each city has a airport and a rail station. You are given two vectors airlines and railways, each of the form [u,v,c], representing that to travel from u to v takes cost c. Now, if you are in a city, you can change your mode of transportation by going from rail station to airport to continue next journey or vice versa. Changing mode of transportation in a city, takes time X. Since to go from railstation to airport or vice versa takes X mins in taxi. Now you are in city src, and want to go to city dest. You can start at either airport or railway station at city src.  Find the minimum cost possible to reach dest from src. If not possible to reach, return -1.
  4.  
  5. Example 1
  6. N = 2
  7. X = 12
  8. src = 0
  9. dest = 1
  10. M1 = 1
  11. airlines[] = {{0, 1, 10}}
  12. M2 = 2
  13. railways[] = {{1, 0, 8},
  14.               {0, 1, 7}}
  15. Output:
  16. 7
  17. Explanation:
  18. We will start from railway station
  19. at the source city and take a train to
  20. reach the destination city.
  21.  
  22. Example 2:
  23. N = 2
  24. X = 12
  25. src = 1
  26. dest = 0
  27. M1 = 1
  28. airlines[] = {{0, 1, 10}}
  29. M2 = 1
  30. railways[] = {{0, 1, 7}}
  31. Output:
  32. -1
  33. Explanation:
  34. There is no way to reach destination
  35. city from source city.
  36.  
  37. Example 3:
  38. N=4
  39. X=1
  40. src = 0
  41. dest = 2
  42. M1 = 5
  43. airlines[] = {{2, 0, 4},{0, 3, 8},{1, 2, 5},{3, 1, 3},{0, 1, 5}}
  44. M2 = 1
  45. railways[] = {{1, 2, 1}}
  46.  
  47. -------------------------------------------------------------------------------------------------------------------------------------
  48. class Solution {
  49.  public:
  50.     vector<vector<pair<int,int>>> adjR; // u={v,time,'type'}  // railways
  51.     vector<vector<pair<int,int>>> adjF;  // flights
  52.    
  53.     long long minTime(int N, int X, int src, int dest, int M1, int M2,
  54.                 vector<vector<int>> airlines, vector<vector<int>> railways){
  55.         adjR.resize(N+1);
  56.         adjF.resize(N+1);
  57.         for(auto airline: airlines){
  58.             int src=airline[0];
  59.             int dest=airline[1];
  60.             int t=airline[2];
  61.             adjF[src].push_back({dest,t});
  62.         }
  63.         for(auto railway: railways){
  64.             int src=railway[0];
  65.             int dest=railway[1];
  66.             int t=railway[2];
  67.             adjR[src].push_back({dest,t});
  68.         }
  69.         priority_queue<pair<long long,pair<int,char>>,vector<pair<long long,pair<int,char>>>,greater<pair<long long,pair<int,char>>>> pq;  //{time,{node,type}}
  70.  
  71.         vector<long long> distF(N+1,LLONG_MAX);
  72.         vector<long long> distR(N+1,LLONG_MAX);
  73.  
  74.         pq.push({0,{src,'f'}});
  75.         pq.push({0,{src,'r'}});
  76.  
  77.         distF[src]=0;
  78.         distR[src]=0;
  79.  
  80.         while(!pq.empty()){
  81.             auto curr=pq.top();
  82.             pq.pop();
  83.            
  84.             long long tt=curr.first;
  85.             int node=curr.second.first;
  86.             char type=curr.second.second;
  87.            
  88.             if(type=='r' && distR[node]<tt){
  89.                 continue;
  90.             }
  91.             if(type=='f' && distF[node]<tt){
  92.                 continue;
  93.             }
  94.            
  95.             if(type=='r'){
  96.                 for(auto nei: adjR[node]){
  97.                     if(distR[nei.first]>distR[node]+nei.second){
  98.                         distR[nei.first]=distR[node]+nei.second;
  99.                         pq.push({distR[nei.first],{nei.first,'r'}});
  100.                     }
  101.                 }
  102.                 for(auto nei: adjF[node]){
  103.                     if(distF[nei.first]>distR[node]+nei.second+X){
  104.                         distF[nei.first]=distR[node]+nei.second+X;
  105.                         pq.push({distF[nei.first],{nei.first,'f'}});
  106.                     }
  107.                 }
  108.             }
  109.             else{
  110.                 for(auto nei: adjF[node]){
  111.                     if(distF[nei.first]>distF[node]+nei.second){
  112.                         distF[nei.first]=distF[node]+nei.second;
  113.                         pq.push({distF[nei.first],{nei.first,'f'}});
  114.                     }
  115.                 }
  116.                 for(auto nei: adjR[node]){
  117.                     if(distR[nei.first]>distF[node]+nei.second+X){
  118.                         distR[nei.first]=distF[node]+nei.second+X;
  119.                         pq.push({distR[nei.first],{nei.first,'r'}});
  120.                     }
  121.                 }
  122.             }
  123.         }
  124.         long long res=min(distR[dest],distF[dest]);
  125.         return res==LLONG_MAX ? -1 : res;
  126.     }
  127. };
Advertisement
Add Comment
Please, Sign In to add comment