Copy Paste 3???
View as PDF
Submit solution
Points:
100
Time limit:
2.0s
PyPy 3
5.0s
Python 3
5.0s
Memory limit:
500M
Problem type
You are given a directed graph with nodes and
edges. What is the shortest-path distance
from node
to every other node?
Input
The first line contains the integers and
.
The following lines contain integers
,
, and
indicating a directed edge from node
to node
with weight
. There may be multiple edges between the same pair of nodes.
The nodes are numbered through
.
It is guaranteed that there is a path from to every other node and that there are no negative
cycles.
Output
Print one line with integers: the shortest-path distances to nodes
,
,
,
,
in that order, separated by spaces.
Constraints
Example 1
Input
3 3
1 2 2
1 3 1
3 2 -1
Output
0 1
Explanation
The shortest path to node is the direct edge of weight
. The shortest path to node
is
with total weight
.
Example 2
Input
10 15
1 2 7
2 3 7
3 4 4
4 5 6
5 6 3
6 7 8
7 8 9
8 9 8
9 10 5
10 1 1
8 10 3
4 6 1
6 9 2
4 5 3
6 10 3
Output
7 14 18 21 19 27 36 21 22
Comments