Leave The Stale Ones In
Dijkstra with a binary heap, written the way it is written when the heap has no decrease-key:
priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<>> pq;
d[s] = 0; pq.push({0, s});
while (!pq.empty()) {
auto [dv, v] = pq.top(); pq.pop();
if (dv != d[v]) continue; // line 5
for (auto [u, w] : adj[v])
if (d[v] + w < d[u]) { d[u] = d[v] + w; pq.push({d[u], u}); }
}
A vertex can sit in the heap several times at several distances, because nothing is ever removed when a shorter path is found. Delete line 5. What is now true?
Sign in to answer questions and track your progress
Sign In