Data Structures & AlgorithmsVery hardICPC regional · graph round

Problem: A solver runs Dijkstra from A on a directed graph. Which graph makes Dijkstra output a wrong shortest distance?

  1. A. Edges A to B weight 2, A to C weight 3, C to B weight 1 (all non-negative)
  2. B. Edges A to B weight 4, B to C weight -1, A to C weight 3
  3. C. Edges A to B weight 1, A to C weight 5, C to B weight -10
  4. D. Edges A to B weight -1 and B to A weight 5 (no negative cycle)

Correct answer: C. Edges A to B weight 1, A to C weight 5, C to B weight -10

Dijkstra finalises B with distance 1 before C is relaxed, but the path A to C to B has weight 5 - 10 = -5. Negative edges break the invariant that a finalised vertex is optimal.

More Coding Interview Problems questions

Exams that ask this topic