File tree 1 file changed +35
-0
lines changed
1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ import sys
2
+ from heapq import *
3
+
4
+ input = sys .stdin .readline
5
+
6
+ vertex , edge = map (int , input ().split ())
7
+ start_vertex = int (input ())
8
+ graph = {v : [] for v in range (1 ,vertex + 1 )}
9
+ for _ in range (edge ) :
10
+ start , end , weight = map (int , input ().split ())
11
+ graph [start ].append ((end , weight ))
12
+
13
+ heap = []
14
+ result = [int (1e9 )] * (vertex + 1 )
15
+ result [start_vertex ] = 0
16
+ heappush (heap , (start_vertex ,0 ))
17
+
18
+ while heap :
19
+
20
+ current_node , current_weight = heappop (heap )
21
+ if result [current_node ] < current_weight :
22
+ continue
23
+ for node , weight in graph [current_node ]:
24
+ distance = current_weight + weight
25
+ if distance < result [node ]:
26
+ result [node ] = distance
27
+ heappush (heap , (node , distance ))
28
+
29
+ for i in range (1 ,vertex + 1 ):
30
+ if result [i ] == int (1e9 ):
31
+ print ('INF' )
32
+ else :
33
+ print (result [i ])
34
+
35
+
You can’t perform that action at this time.
0 commit comments