Problems > Longest Path in a DAG

Longest Path in a DAG

Difficulty: 1600 5000 ms 128 MB

dp graphs

Print the maximum total edge weight of any directed path in a weighted DAG.

Input: the first line has N and M; each of the next M lines has u v w (the graph is acyclic).

Output: the longest path weight (0 if no edges).

Constraints:

  • 2 ≤ N ≤ 105
  • 1 ≤ M ≤ 2×105
  • 1 ≤ w ≤ 1000

Sample Tests

Input 1
4 4
1 2 3
2 3 4
1 3 2
3 4 1
Output 1
8
Input 2
2 1
1 2 5
Output 2
5

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.