Graph Theory37 sections · 1633 units
Open in Course

CSES 1673 High Score - Algorithm

(Step-by-step approach)

1.1. Negate all edge weights to convert the problem.

2.2. Run Bellman-Ford from node 11 for n1n-1 iterations.

3.3. Check for negative cycles using an nn-th iteration.

4.4. If a cycle exists, mark all nodes improved in the nn-th iteration.

5.5. Propagate the cycle effect using BFS to find all affected nodes.

6.6. Check if node nn is affected.

If yes, output 1-1. Otherwise, output -dist[n]. The core step is propagating cycle effects to all reachable nodes, not the nodes directly in the cycle.