Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div 2 D

Directed weighted graph on vertices 0 to 5 with edges (u→v (w)): 5→4 (1), 3→2 (6), 4→3 (7), 3→5 (6), 0→2 (4), 5→0 (5), 0→5 (1), 4→0 (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. 75
  2. B. 31
  3. C. 86
  4. D. 16

Correct answer: A. 75

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic