Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B

Floyd–Warshall runs on vertices 1 to 5 with these arcs u→v (weight): 1→2 (4), 1→3 (1), 2→3 (11), 2→4 (2), 2→5 (2), 3→1 (6), 3→2 (4), 4→2 (3), 5→2 (7), 5→4 (11). 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][3]?

  1. A. ∞
  2. B. 11
  3. C. 3
  4. D. 14

Correct answer: D. 14

After rounds k = 1..2, only vertices 1 to 2 may sit inside a path. 4→2 (3) + 2→3 (11) = 14 becomes available in round k = 2. After round 1 alone the entry is still ∞, 11 is just the arc 2→3 and 3 is just the arc 4→2.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic