Data Structures & AlgorithmsVery hardCodeforces Div2 B · DP warm-up

Problem: A graph may contain negative edge weights but no negative cycle. Which single-source shortest path algorithm handles this correctly, and what is its time complexity?

  1. A. Dijkstra's algorithm, O((V+E) log V)
  2. B. Bellman-Ford, O(VE)
  3. C. Floyd-Warshall, O(V^2)
  4. D. BFS, O(V+E)

Correct answer: B. Bellman-Ford, O(VE)

Bellman-Ford relaxes every edge V-1 times, correctly propagating negative weights (and can detect a negative cycle with one extra pass), at the cost of O(VE) instead of Dijkstra's faster but negative-weight-unsafe approach.

More Coding Interview Problems questions

Exams that ask this topic