Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div 2 D

Directed weighted graph on vertices 0 to 4 with edges (u→v (w)): 0→1 (9), 4→0 (6), 2→1 (5), 0→3 (9), 2→4 (8), 3→1 (8), 1→3 (9), 4→1 (7), 1→4 (2), 4→2 (6), 3→4 (1). Compute the sum of the shortest-path distances over every ordered pair (i, j) with i ≠ j where j is reachable from i. What is that sum?

  1. A. 70
  2. B. 167
  3. C. 114
  4. D. 20

Correct answer: B. 167

Floyd–Warshall (checked against Dijkstra from every source) gives a total of 167. Adding only the direct edge weights gives 70, and ignoring edge directions gives 114; the reachable pair count 20 is not a sum at all.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic