Graph Theory37 sections · 1633 units
Open in Course

CSES 1673 High Score - Walkthrough

(Trace execution)

Graph: 1→21 \to 2 (score 33), 2→32 \to 3 (score 44), 3→23 \to 2 (score 55), 2→42 \to 4 (score 11), n=4n = 4. After negating: 1→21 \to 2 (−3-3), 2→32 \to 3 (−4-4), 3→23 \to 2 (−5-5), 2→42 \to 4 (−1-1). Run Bellman-Ford: after 33 iterations, dist[2] = -12, dist[3] = -7, dist[4] = -4.

The 44-th iteration improves dist[3] (to −16-16) and dist[4] (to −13-13) due to cycle 2↔32 \leftrightarrow 3. Mark all nodes that improved and all nodes reachable from them.

Check two-way reachability: node 22 (the cycle) is reachable from node 11 (direct edge 1→21 \to 2). Node 44 is reachable from node 22 (direct edge 2→42 \to 4). Both checks pass. Output −1-1.