Data Structures & AlgorithmsHardGATE CS · ICPC regional

Directed weighted graph on vertices A, B, C, D with edges (u→v, w): C→A (2); D→B (1); C→D (7); B→A (5); B→D (5); A→B (8); D→A (3); C→B (6). 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. 50
  2. B. 3
  3. C. 33
  4. D. 9

Correct answer: A. 50

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic