File tree Expand file tree Collapse file tree 1 file changed +10
-1
lines changed Expand file tree Collapse file tree 1 file changed +10
-1
lines changed Original file line number Diff line number Diff line change @@ -15,17 +15,26 @@ class Solution {
15
15
16
16
int result = 0 ;
17
17
unordered_set<int > lookup;
18
+ unordered_map<int , int > best;
18
19
priority_queue<P, vector<P>, greater<P>> min_heap;
19
20
min_heap.emplace (0 , K - 1 );
20
21
while (!min_heap.empty () && lookup.size () != N) {
21
22
int u;
22
23
tie (result, u) = min_heap.top (); min_heap.pop ();
23
24
lookup.emplace (u);
25
+ if (best.count (u) &&
26
+ best[u] < result) {
27
+ continue ;
28
+ }
24
29
for (const auto & kvp : adj[u]) {
25
30
int v, w;
26
31
tie (v, w) = kvp;
27
32
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
+ }
29
38
}
30
39
}
31
40
return lookup.size () == N ? result : -1 ;
You can’t perform that action at this time.
0 commit comments