Skip to content

Commit aa04a15

Browse files
authored
Update network-delay-time.cpp
1 parent f389ff7 commit aa04a15

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

C++/network-delay-time.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@ class Solution {
1515

1616
int result = 0;
1717
unordered_set<int> lookup;
18+
unordered_map<int, int> best;
1819
priority_queue<P, vector<P>, greater<P>> min_heap;
1920
min_heap.emplace(0, K - 1);
2021
while (!min_heap.empty() && lookup.size() != N) {
2122
int u;
2223
tie(result, u) = min_heap.top(); min_heap.pop();
2324
lookup.emplace(u);
25+
if (best.count(u) &&
26+
best[u] < result) {
27+
continue;
28+
}
2429
for (const auto& kvp : adj[u]) {
2530
int v, w;
2631
tie(v, w) = kvp;
2732
if (lookup.count(v)) continue;
28-
min_heap.emplace(result + w, v);
33+
if (!best.count(v) ||
34+
result + w < best[v]) {
35+
best[v] = result + w;
36+
min_heap.emplace(result + w, v);
37+
}
2938
}
3039
}
3140
return lookup.size() == N ? result : -1;

0 commit comments

Comments
 (0)