Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 C

Directed graph on vertices 0 to 8 with weighted edges (u→v (w)): 4→8 (6), 6→1 (7), 6→3 (1), 8→7 (12), 5→3 (4), 2→7 (8), 3→1 (4), 4→1 (9), 7→2 (11), 5→7 (8), 4→5 (10), 1→4 (1), 2→1 (11), 7→8 (5), 6→7 (9), 3→2 (6), 8→1 (5), 5→6 (10), 1→3 (9), 0→3 (9), 8→5 (7), 5→4 (3), 3→7 (10), 6→4 (10). What is the length of a shortest path from 0 to 8, where the length is the sum of edge weights?

  1. A. 24
  2. B. 18
  3. C. 20
  4. D. unreachable

Correct answer: C. 20

Dijkstra finalises vertices in increasing distance and gives 20 via 0 → 3 → 1 → 4 → 8. Counting edges instead of weights picks a route with 3 edge(s) and weight 24, which is the common slip.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic