Data Structures & AlgorithmsHardCodeforces Div2 D · shortest path

Problem: After running Floyd-Warshall on a directed graph with possibly negative edges, which check correctly detects that vertex i lies on a negative cycle?

  1. A. dist[i][i] < 0
  2. B. dist[0][i] < 0
  3. C. dist[i][i] == i
  4. D. dist[i][j] < 0 for every j

Correct answer: A. dist[i][i] < 0

If i lies on a negative cycle, the DP can route a path from i back to i with negative total weight, so dist[i][i] becomes negative. A negative distance from vertex 0 alone does not imply a cycle.

More Coding Interview Problems questions

Exams that ask this topic