Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional

Problem: Floyd–Warshall is run on 4 vertices with directed edges 0→1 (3), 1→2 (4), 0→2 (9), 2→3 (1), 3→0 (2), 1→3 (5). Unreachable pairs contribute nothing. What is the sum of all shortest-path distances d(i, j) over ordered pairs with i ≠ j that are reachable?

  1. A. 24
  2. B. 60
  3. C. 36
  4. D. 62

Correct answer: B. 60

After Floyd–Warshall every vertex reaches every other (the cycle through 3→0 closes the graph), and the 12 shortest distances sum to 60. Summing only the direct edge weights gives 24, treating the edges as undirected gives 36, and keeping the direct 0→2 edge of 9 instead of 0→1→2 = 7 gives 62.

More Coding Interview Problems questions

Exams that ask this topic