-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_787_CheapestFlightsWithinKStops.cpp
156 lines (127 loc) · 4.92 KB
/
LC_787_CheapestFlightsWithinKStops.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
https://leetcode.com/problems/cheapest-flights-within-k-stops/
787. Cheapest Flights Within K Stops
*/
class Solution {
public:
// int Bellman(int n, vector<vector<int>>& flights, int src, int dst, int k) {
// //Using Bellman Ford
// vector<int> dist(n, 1e9);
// int edges = flights.size();
// dist[src] = 0;
// for(int v=0; v<=k; v++)
// {
// vector<int> ndist = dist;
// for(int e=0; e<edges; e++)
// {
// int u = flights[e][0], v = flights[e][1], p = flights[e][2];
// if(dist[u] + p < ndist[v])
// ndist[v] = dist[u]+p;
// }
// dist = ndist;
// }
// // for(int x: dist) cout<<x<<" "; cout<<endl;
// return dist[dst] == 1e9 ? -1 : dist[dst];
// }
typedef pair<int, pair<int,int>> pii; //cost, src, stops
typedef pair<int,int> pi; // dst, price
int dijkstra_1(int n, int src, int dst, int k, vector<pi> adj[]) {
priority_queue<pii, vector<pii>, greater<>> pq;
pq.push({0, {src, 0}});// cost, src, stops
vector<int> cost(n, 1e9);
vector<int> stops(n, 1e9);
cost[src] = 0; stops[src] = 0;
while(!pq.empty())
{
int c = pq.top().first; //cost of u
int u = pq.top().second.first; // u
int s = pq.top().second.second; pq.pop();// stops of u
if(s >= k+1) continue;
for(auto [v, price]: adj[u])
{
if(v == dst)
{
if(c + price < cost[v] and s<=k)
{
cost[v] =c+price; stops[v] = s;
pq.push({cost[v],{v, stops[v]}});
}
} // either price should be less or hops is less then insert into priority queue
else if((c + price < cost[v]) or (1 + s < stops[v]))
{
cost[v] =c+price; stops[v] = 1+s;
pq.push({cost[v],{v, stops[v]}});
}
}
}//while
// for(int i=0; i<n; i++) cout<<cost[i]<<" "; cout<<endl;
// for(int i=0; i<n; i++) cout<<stops[i]<<" "; cout<<endl;
if(cost[dst] == 1e9)
return -1;
else
return cost[dst];
}
int dijkstra_2(int n, int src, int dst, int k, vector<pi> adj[]) {
priority_queue<pii, vector<pii>, greater<>> pq;
pq.push({-1, {0, src}});// level, cost, src
vector<int> cost(n, 1e9);
cost[src] = 0;
while(!pq.empty())
{
int l = pq.top().first;
int c = pq.top().second.first;
int u = pq.top().second.second; pq.pop();
if(l+1 > k) continue;
for(auto [v, price]: adj[u])
{
if(c + price < cost[v] )
{
cost[v] =c+price;
pq.push({l+1,{cost[v], v}});
}
}
}//while
// for(int i=0; i<n; i++) cout<<cost[i]<<" "; cout<<endl;
// for(int i=0; i<n; i++) cout<<stops[i]<<" "; cout<<endl;
if(cost[dst] == 1e9)
return -1;
else
return cost[dst];
}
int dijkstra_3(int n, int src, int dst, int k, vector<pi> adj[]) {
priority_queue<pi> pq; // MAX HEAP
pq.push({0, src});// cost, src
vector<int> cost(n, 1e9), level(n, 1e9);
cost[src] = 0; level[src] = -1;
while(!pq.empty())
{
auto [c, u] = pq.top(); pq.pop();
if(level[u] == k) continue;
// cout<<u<<" "<<c<<" "<<level[u]<<") ";
for(auto [v, price]: adj[u])
{
if(c + price < cost[v] )
{
level[v] = 1 + level[u];
cost[v] =c+price;
pq.push({cost[v], v});
}
}
}//while
for(int i=0; i<n; i++) cout<<cost[i]<<" "; cout<<endl;
for(int i=0; i<n; i++) cout<<level[i]<<" "; cout<<endl;
if(cost[dst] == 1e9)
return -1;
else
return cost[dst];
}
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
// return Bellman(n, flights, src, dst, k);
vector<pi> adj[n];
for(auto &e: flights)
adj[e[0]].push_back({e[1], e[2]});
// return dijkstra_1(n, src, dst, k, adj);
return dijkstra_2(n, src, dst, k, adj); // min heap + level
// return dijkstra_3(n, src, dst, k, adj); //max heap + level
}
};