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 (6), 1→3 (5), 1→5 (6), 2→3 (5), 2→4 (11), 3→1 (11), 3→2 (3), 3→4 (6), 3→5 (1), 4→5 (9), 5→1 (6), 5→2 (11), 5→3 (1). 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 1 have finished, what is dist[5][2]?

  1. A. 11
  2. B. 4
  3. C. 12
  4. D. 13

Correct answer: A. 11

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic