Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional

Directed weighted graph on vertices E, C, A, B, D (source E, target D) with edges: E→C (2); E→A (3); A→C (-3); C→D (3); A→D (3); E→D (18); E→B (7). Run Dijkstra from E 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. 3
  2. B. 18
  3. C. 6
  4. D. 5

Correct answer: D. 5

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