Shortest Path in a DAG
Find the shortest path from a source to all nodes in a weighted DAG using topological sort.
Input format
The first line contains N, M, and SRC. The next M lines contain directed triples (u, v, w) with weights 1–20.
Output format
Print N distances from SRC; -1 if unreachable.
Constraints
- Values fit in a 64-bit signed integer
- Trailing whitespace and a trailing newline are ignored by the judge
Read from stdin, write to stdout. Sample cases below show the exact format.
Sample cases
Example 1
Input
6 7 0
0 1 2
0 4 1
1 2 3
4 2 2
2 3 6
4 5 4
5 3 1Expected output
0 2 3 6 1 5Example 2
Input
4 2 0
0 1 5
2 3 7Expected output
0 5 -1 -1