-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0787.cpp
More file actions
30 lines (26 loc) · 863 Bytes
/
Copy path0787.cpp
File metadata and controls
30 lines (26 loc) · 863 Bytes
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
class Solution {
struct edge {
int d, w, s;
edge(int d, int w, int s = -1) : d(d), w(w), s(s) {}
friend bool operator<(const edge &e1, const edge &e2) { return e1.w > e2.w; }
};
public:
int findCheapestPrice(int n, vector<vector<int>> &flights, int src, int dst, int k) {
vector<vector<edge>> adj(n);
for (auto &f : flights)
adj[f[0]].push_back({f[1], f[2]});
vector<int> stop(n, INT_MAX);
priority_queue<edge> pq;
pq.push({src, 0, 0});
while (!pq.empty()) {
auto [d, w, s] = pq.top();
pq.pop();
if (s > stop[d] || s > k + 1) continue;
stop[d] = s;
if (d == dst) return w;
for (auto [d1, w1, _] : adj[d])
pq.push({d1, w + w1, s + 1});
}
return -1;
}
};