Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 C

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

  1. A. 19
  2. B. 10
  3. C. unreachable
  4. D. 12

Correct answer: D. 12

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic