Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional

Bellman–Ford is run from source O on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: L→O (6), O→L (6), M→O (7), L→M (6), L→Y (5), M→L (2), M→Y (8), Y→O (2), O→M (2). One pass relaxes every arc once, in the listed order. Passes are repeated until a complete pass changes no distance. Counting the final pass that changes nothing, how many passes run in total?

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

Correct answer: B. 4

The last pass changes no distance, so it is counted; the passes before it changed something, 3 of them. The bound n − 1 = 3 counts only the passes needed in the worst case.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic