Data Structures & AlgorithmsVery hardICPC regional · GATE CS top band

Directed weighted graph on vertices B, F, A, C, D (source B, target D) with edges: B→F (2); B→A (5); A→F (-4); F→D (1); A→D (3); B→D (12); B→C (7). 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. 8
  2. B. 12
  3. C. 3
  4. D. 2

Correct answer: C. 3

Dijkstra finalises F at distance 2 before the negative edge from A is seen, so it reports 3. The true shortest distance is 2, 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