Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 C

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

  1. A. 15
  2. B. 16
  3. C. 10
  4. D. 19

Correct answer: A. 15

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic