Skip to content

Commit 6e34235

Browse files
authored
Update cheapest-flights-within-k-stops.cpp
1 parent ac6f4f7 commit 6e34235

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

C++/cheapest-flights-within-k-stops.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class Solution {
1515
unordered_map<int, unordered_map<int, int>> best;
1616
using T = tuple<int, int, int>;
1717
priority_queue<T, vector<T>, greater<T>> min_heap;
18-
min_heap.emplace(0, K + 1, src);
18+
min_heap.emplace(0, src, K + 1);
1919
while (!min_heap.empty()) {
20-
int result, k, u;
21-
tie(result, k, u) = min_heap.top(); min_heap.pop();
20+
int result, u, k;
21+
tie(result, u, k) = min_heap.top(); min_heap.pop();
2222
if (k < 0 ||
23-
(best.count(k) && best[k].count(u) && best[k][u] < result)) {
23+
(best.count(u) && best[u].count(k) && best[u][k] < result)) {
2424
continue;
2525
}
2626
if (u == dst) {
@@ -29,11 +29,11 @@ class Solution {
2929
for (const auto& kvp : adj[u]) {
3030
int v, w;
3131
tie(v, w) = kvp;
32-
if (!best.count(k - 1) ||
33-
!best[k - 1].count(v) ||
34-
result + w < best[k - 1][v]) {
35-
best[k - 1][v] = result + w;
36-
min_heap.emplace(result + w, k - 1, v);
32+
if (!best.count(v) ||
33+
!best[v].count(k - 1) ||
34+
result + w < best[v][k - 1]) {
35+
best[v][k - 1] = result + w;
36+
min_heap.emplace(result + w, v, k - 1);
3737
}
3838
}
3939
}

0 commit comments

Comments
 (0)