From a2f520431636d44eb12f4ee649ac28218a4e303f Mon Sep 17 00:00:00 2001 From: bastifwp <128760008+bastifwp@users.noreply.github.com> Date: Sat, 16 May 2026 22:07:42 -0400 Subject: [PATCH] Fix Bellman-Ford condition to update distances Without the Fix, nodes unreachable from the source may be updated. --- content/graphs/shortest-paths/bellman-ford.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/graphs/shortest-paths/bellman-ford.cpp b/content/graphs/shortest-paths/bellman-ford.cpp index 546ba27..899dbac 100644 --- a/content/graphs/shortest-paths/bellman-ford.cpp +++ b/content/graphs/shortest-paths/bellman-ford.cpp @@ -12,7 +12,7 @@ struct BellmanFord { for (int i = 1; i <= n; i++) { last_updated = -1; for (Edge &e : G) - if (dist[e.from] + e.weight < dist[e.to]) { + if (dist[e.from] != INF && dist[e.from] + e.weight < dist[e.to]) { dist[e.to] = dist[e.from] + e.weight; p[e.to] = e.from; last_updated = e.to; } @@ -29,4 +29,4 @@ struct BellmanFord { reverse(cycle.begin(), cycle.end()); return true; } -}; \ No newline at end of file +};