Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://practice.geeksforgeeks.org/contest/gfg-weekly-coding-contest-102/problems/
- 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.
- Example 1
- N = 2
- X = 12
- src = 0
- dest = 1
- M1 = 1
- airlines[] = {{0, 1, 10}}
- M2 = 2
- railways[] = {{1, 0, 8},
- {0, 1, 7}}
- Output:
- 7
- Explanation:
- We will start from railway station
- at the source city and take a train to
- reach the destination city.
- Example 2:
- N = 2
- X = 12
- src = 1
- dest = 0
- M1 = 1
- airlines[] = {{0, 1, 10}}
- M2 = 1
- railways[] = {{0, 1, 7}}
- Output:
- -1
- Explanation:
- There is no way to reach destination
- city from source city.
- Example 3:
- N=4
- X=1
- src = 0
- dest = 2
- M1 = 5
- airlines[] = {{2, 0, 4},{0, 3, 8},{1, 2, 5},{3, 1, 3},{0, 1, 5}}
- M2 = 1
- railways[] = {{1, 2, 1}}
- -------------------------------------------------------------------------------------------------------------------------------------
- class Solution {
- public:
- vector<vector<pair<int,int>>> adjR; // u={v,time,'type'} // railways
- vector<vector<pair<int,int>>> adjF; // flights
- long long minTime(int N, int X, int src, int dest, int M1, int M2,
- vector<vector<int>> airlines, vector<vector<int>> railways){
- adjR.resize(N+1);
- adjF.resize(N+1);
- for(auto airline: airlines){
- int src=airline[0];
- int dest=airline[1];
- int t=airline[2];
- adjF[src].push_back({dest,t});
- }
- for(auto railway: railways){
- int src=railway[0];
- int dest=railway[1];
- int t=railway[2];
- adjR[src].push_back({dest,t});
- }
- 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}}
- vector<long long> distF(N+1,LLONG_MAX);
- vector<long long> distR(N+1,LLONG_MAX);
- pq.push({0,{src,'f'}});
- pq.push({0,{src,'r'}});
- distF[src]=0;
- distR[src]=0;
- while(!pq.empty()){
- auto curr=pq.top();
- pq.pop();
- long long tt=curr.first;
- int node=curr.second.first;
- char type=curr.second.second;
- if(type=='r' && distR[node]<tt){
- continue;
- }
- if(type=='f' && distF[node]<tt){
- continue;
- }
- if(type=='r'){
- for(auto nei: adjR[node]){
- if(distR[nei.first]>distR[node]+nei.second){
- distR[nei.first]=distR[node]+nei.second;
- pq.push({distR[nei.first],{nei.first,'r'}});
- }
- }
- for(auto nei: adjF[node]){
- if(distF[nei.first]>distR[node]+nei.second+X){
- distF[nei.first]=distR[node]+nei.second+X;
- pq.push({distF[nei.first],{nei.first,'f'}});
- }
- }
- }
- else{
- for(auto nei: adjF[node]){
- if(distF[nei.first]>distF[node]+nei.second){
- distF[nei.first]=distF[node]+nei.second;
- pq.push({distF[nei.first],{nei.first,'f'}});
- }
- }
- for(auto nei: adjR[node]){
- if(distR[nei.first]>distF[node]+nei.second+X){
- distR[nei.first]=distF[node]+nei.second+X;
- pq.push({distR[nei.first],{nei.first,'r'}});
- }
- }
- }
- }
- long long res=min(distR[dest],distF[dest]);
- return res==LLONG_MAX ? -1 : res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment