Dijkstra Shortest Path
Hardjavapythonccppjavascript
Given a directed graph with non-negative edge weights, find the shortest distance from a source vertex S to every vertex.
Input Format
The first line contains two integers V and E. Each of the next E lines contains a directed edge u v w (from u to v with weight w). The last line contains the source vertex S.
Output Format
Print V space-separated integers: the shortest distance from S to vertex 0, 1, ..., V-1. Use -1 for a vertex that cannot be reached.
Example 1
Input
4 4 0 1 4 0 2 1 2 1 2 1 3 1 0
Output
0 3 1 4
Explanation: The shortest route from 0 to 1 is 0 -> 2 -> 1 with cost 3.
- 1 <= V <= 100000
- 0 <= E <= 200000
- 0 <= w <= 1000000000
- 0 <= u, v, S < V