Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

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

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

Correct answer: C. 2

Only intermediate vertices 1 to 4 are allowed in the middle of paths at this point, which gives 2. Stopping one iteration earlier gives 2, and the final distance is 2.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic