Data Structures & AlgorithmsVery hardCodeforces Div 2 D · ICPC regional

Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→4 (4), 2→1 (1), 2→3 (4), 3→1 (5), 3→4 (10), 4→1 (6), 4→2 (6), 4→3 (3). dist[i][i] = 0, dist[i][j] is the arc weight, or ∞ when there is no arc. The outer loop runs over k = 1, 2, … in that order. After the iterations for k = 1 through 2 have finished, what is dist[4][1]?

  1. A. 4
  2. B. 15
  3. C. 1
  4. D. 6

Correct answer: D. 6

After rounds k = 1..2, only vertices 1 to 2 may sit inside a path. The direct arc 4→1 (6) already beats 4→2→1 = 7, so dist[4][1] stays 6. 4 reads the reverse entry dist[1][4], 15 is the route through vertex 3 that is not yet allowed, and 1 is the arc 2→1 alone.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic