forked from Aadi71/Data-Structures-and-Algorithms-with-Aadi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cheapest-flights-within-k-stops.cpp
86 lines (63 loc) · 2.11 KB
/
cheapest-flights-within-k-stops.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// https://leetcode.com/problems/cheapest-flights-within-k-stops/
// BFS - Not Accepted(TLE)
class Solution {
public:
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
vector<pair<int, int>> adj[n];
for(int i = 0; i<flights.size(); i++){
adj[flights[i][0]].push_back({flights[i][1], flights[i][2]});
}
queue<pair<int, int>> q;
q.push({src, 0});
int mini = INT_MAX;
while(!q.empty() && k >= 0){
int n = q.size();
while(n--){
pair<int, int> f = q.front();
q.pop();
int node = f.first; int dist = f.second;
for(auto it: adj[node]){
int a = it.first, b = it.second;
if(mini <= dist + b) continue;
q.push({a, dist + b});
if(a == dst) mini = min(mini, dist + b);
}
}
k--;
}
if(mini != INT_MAX) return mini;
return -1;
}
};
// Dijkstra Algorithm using queue only
class Solution {
public:
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
vector<pair<int,int>> adj[n];
for(auto it : flights) {
adj[it[0]].push_back({it[1],it[2]});
}
vector<int> dist(n, INT_MAX);
dist[src] = 0;
// {stops, {node,cost}}
queue<pair<int,pair<int,int>>> q;
q.push({0,{src,0}});
while(!q.empty()){
int stops = q.front().first;
int node = q.front().second.first;
int cost = q.front().second.second;
q.pop();
if(stops > k) break;
for(auto it : adj[node]) {
int adjNode = it.first;
int adjWt = it.second;
if(cost + adjWt < dist[adjNode]) {
dist[adjNode] = cost + adjWt;
q.push({stops+1, {adjNode, dist[adjNode]}});
}
}
}
if(dist[dst] == INT_MAX) return -1;
return dist[dst];
}
};