Graph Theory37 sections · 1633 units
Open in Course

Edge Case - Unreachable Destination

Handling IMPOSSIBLE

If city nn is unreachable from city 11, dp[n] remains -\infty after processing all nodes. You must check this before outputting the answer.

Return "IMPOSSIBLE" or similar when dp[n] equals your sentinel value. If you forget this check, you might output -\infty as a large negative number, which is wrong.

This edge case appears in many shortest/longest path problems. Always verify reachability. In a DAG, unreachable nodes keep their initial values. The topological order guarantees you process all reachable nodes, but cannot create paths that do not exist.