-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheapest Flights Within K Stops S.cpp
35 lines (34 loc) · 1.2 KB
/
Cheapest Flights Within K Stops S.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
vector<vector<int>> v(n);
map<pair<int,int>,int> mpp; // stores the prices of all routes
for(int i=0;i<flights.size();i++){
v[flights[i][0]].push_back(flights[i][1]);
mpp[{flights[i][0],flights[i][1]}] = flights[i][2];
}
queue<pair<int,int>> q;
q.push({src,0});
vector<int> vis(n,INT_MAX);
vis[src] = 0;
int dist = 0;
while(!q.empty() and dist<k+1){
int size = q.size();
while(size--){
auto check = q.front();
q.pop();
for(auto i : v[check.first]){
int cost = mpp[{check.first,i}];
cout<<cost<<endl;
int total = check.second+cost;
if(vis[i]>total){
vis[i] = total;
q.push({i,vis[i]});
}
}
}
dist++;
}
return vis[dst] == INT_MAX ? -1 : vis[dst];
}
};