Data Structures & AlgorithmsVery hardCodeforces Div 2 D · ICPC regional

Bellman–Ford from source 0 (dist[0] = 0, every other distance ∞). One pass relaxes every listed edge once, in the listed order. Edges (u→v (w)): 0→3 (6), 5→4 (1), 3→1 (0), 4→3 (3), 4→1 (4), 4→2 (3), 0→1 (2), 4→0 (2), 0→4 (3). Passes are repeated until a complete pass changes no distance; that final no-change pass is counted. How many passes are performed, or 'never stabilises' if no such pass ever occurs?

  1. A. 3
  2. B. 4
  3. C. 2
  4. D. 5

Correct answer: A. 3

Pass 3 is the first pass that changes nothing, so 3 passes are performed in total, the final no-change pass included. A negative cycle reachable from 0 would make the process never stabilise, and reversing the edge list changes the count, so the order is part of the question.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic