Correct But Exponential
To fix the negative-edge bug, a teammate deletes the visited array entirely and keeps only the lazy staleness check, so a vertex is allowed back out of the heap whenever its distance improves:
while (!q.empty()) {
auto [d_v, v] = q.top(); q.pop();
if (d_v != dist[v]) continue; // stale entry, skip
for (auto [to, w] : adj[v]) {
if (dist[v] + w < dist[to]) {
dist[to] = dist[v] + w;
q.push({dist[to], to}); // may re-enter, even if popped before
}
}
}
The input graph has negative edges but no negative cycle. Which is true of this version?
Sign in to answer questions and track your progress
Sign In