Real callers want both answers at once: the distances and whether they can be trusted. Combine Steps 1 and 2 into a single clear contract.
Implement bellman_ford_safe(n, edges, source). Return a tuple (dist, neg) where:
Algorithm:
bellman_ford and has_negative_cycle from earlier steps are provided.
def bellman_ford(n, edges, source):
dist = [float('inf')] * n
dist[source] = 0
for _ in range(n - 1):
for u, v, w in edges:
if dist[u] != float('inf') and dist[u] + w < dist[v]:
dist[v] = dist[u] + w
return dist
def has_negative_cycle(n, edges):
dist = [0] * n
for _ in range(n - 1):
for u, v, w in edges:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
for u, v, w in edges:
if dist[u] + w < dist[v]:
return True
return False
def bellman_ford_safe(n, edges, source):
"""Return (dist, neg): distances from source and whether a negative cycle affects them."""
# TODO: relax n - 1 times, then set neg from one extra pass.
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Real callers want both answers at once: the distances and whether they can be trusted. Combine Steps 1 and 2 into a single clear contract.
Implement bellman_ford_safe(n, edges, source). Return a tuple (dist, neg) where:
Algorithm:
bellman_ford and has_negative_cycle from earlier steps are provided.
Press Run Tests or ββ© to check your solution.