#D4820. Single Source Shortest Path II
Single Source Shortest Path II
Single Source Shortest Path II
For a given weighted graph , find the shortest path from a source to each vertex. For each vertex , print the total weight of edges on the shortest path from vertex to .
Constraints
- All vertices are reachable from vertex
Input
In the first line, an integer denoting the number of vertices in is given. In the following lines, adjacency lists for each vertex are respectively given in the following format:
...
Vertices in are named with IDs . is ID of the target vertex and denotes its degree. denote IDs of vertices adjacent to and denotes the weight of a directed edge connecting and (from to ).
Output
For each vertex, print its ID and the distance separated by a space character in a line respectively. Print in order of vertex IDs.
Example
Input
5 0 3 2 3 3 1 1 2 1 2 0 2 3 4 2 3 0 3 3 1 4 1 3 4 2 1 0 1 1 4 4 3 4 2 2 1 3 3
Output
0 0 1 2 2 2 3 1 4 3
inputFormat
Input
In the first line, an integer denoting the number of vertices in is given. In the following lines, adjacency lists for each vertex are respectively given in the following format:
...
Vertices in are named with IDs . is ID of the target vertex and denotes its degree. denote IDs of vertices adjacent to and denotes the weight of a directed edge connecting and (from to ).
outputFormat
Output
For each vertex, print its ID and the distance separated by a space character in a line respectively. Print in order of vertex IDs.
Example
Input
5 0 3 2 3 3 1 1 2 1 2 0 2 3 4 2 3 0 3 3 1 4 1 3 4 2 1 0 1 1 4 4 3 4 2 2 1 3 3
Output
0 0 1 2 2 2 3 1 4 3
样例
5
0 3 2 3 3 1 1 2
1 2 0 2 3 4
2 3 0 3 3 1 4 1
3 4 2 1 0 1 1 4 4 3
4 2 2 1 3 3
0 0
1 2
2 2
3 1
4 3
</p>