Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

Directed weighted graph on vertices B, F, C, A, D (source B, target D) with edges: B→F (5); B→C (6); C→F (-3); F→D (1); C→D (4); B→D (18); B→A (5). Run Dijkstra from B with a min-heap: a vertex is finalised the first time it is popped, its outgoing edges are relaxed then, and a finalised vertex's distance is never changed afterwards (edges into finalised vertices are ignored). What distance to D does it report?

  1. A. 4
  2. B. 6
  3. C. 18
  4. D. 10

Correct answer: B. 6

Dijkstra finalises F at distance 5 before the negative edge from C is seen, so it reports 6. The true shortest distance is 4, which goes through the negative edge; clamping negatives to 0 or using their absolute values are other wrong models.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic