Graph Theory37 sections · 1633 units
Open in Course

Defining the DP State

What dp[u] means

You have a directed graph with weighted edges (can be negative). Find the maximum-score path from 11 to nn. If there is a positive cycle reachable from 11 that can reach nn, the answer is infinite. Otherwise, use DP on the DAG formed by nodes reachable from 11.

Process in topological order, track dp[v] = max score to reach vv. Detect positive cycles using a modified DFS that tracks nodes in the current recursion stack.