Cheapest Flights Within K Stops LeetCode solution in C++

1
class Solution {
2
public:
3
#define f first
4
#define s second
5
int findCheapestPrice(int n, vector<vector<int>> &flights, int src, int dst, int k) {
6
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> pq;
7
unordered_map<int, vector<pair<int, int>>> g;
8

9
for (auto &f : flights) {
10
g[f[0]].push_back({f[1], f[2]});
11
}
12
vector<int> dis(n, INT_MAX);
13
pq.push({0, src, 0});
14
while (!pq.empty()) {
15
int c = pq.top()[0];
16
int cur = pq.top()[1];
17
int lvl = pq.top()[2];
18
pq.pop();
19
if (cur == dst) return c;
20
if (lvl > k || lvl >= dis[cur]) continue;
21
dis[cur] = lvl;
22
for (auto &nei : g[cur]) {
23
pq.push({c + nei.s, nei.f, lvl + 1});
24
}
25
}
26
return -1;
27
}
28
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0