File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import heapq
12from collections import defaultdict
23
34def main ():
45 n , m = map (int , input ().split ())
56
6- countryMap = defaultdict (list )
7+ cityMap = defaultdict (list )
78
89 for _ in range (m ):
9- A_i , B_i , C_i = map (int , input ().strip ().split ())
10- countryMap [A_i ].append (C_i )
11- countryMap [B_i ].append (C_i )
10+ a , b , c = map (int , input ().split ())
11+ cityMap [a ].append ((b , c ))
12+ cityMap [b ].append ((a , c ))
13+
14+ dist = [float ('inf' )] * (n + 1 )
15+ dist [1 ] = 0
16+
17+ pq = []
18+ heapq .heappush (pq , (0 , 1 ))
19+
20+ while pq :
21+ cost , now = heapq .heappop (pq )
22+
23+ if dist [now ] < cost :
24+ continue
25+
26+ for next_node , weight in cityMap [now ]:
27+ new_cost = cost + weight
28+ if new_cost < dist [next_node ]:
29+ dist [next_node ] = new_cost
30+ heapq .heappush (pq , (new_cost , next_node ))
31+
32+ print (dist [n ])
1233
1334if __name__ == "__main__" :
1435 main ()
You can’t perform that action at this time.
0 commit comments