Data Structures & AlgorithmsHardGATE CS · ICPC regional

Directed weighted graph on vertices A, B, C, D, E with edges (u→v, w): A→D (3); E→D (4); A→E (3); C→A (8); A→C (5); D→C (6); E→C (9); B→E (7); B→A (1). Let d(x, y) be the shortest-path distance from x to y, and consider every ORDERED pair (x, y) with x ≠ y for which y is reachable from x. What is the sum of d(x, y) over those pairs?

  1. A. 64
  2. B. 16
  3. C. 4
  4. D. 123

Correct answer: D. 123

Floyd–Warshall gives the reachable ordered pairs, and their distances add up to 123. The number of unreachable pairs (4) and the count of reachable pairs (16) are the wrong quantities.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic