https://www.acmicpc.net/problem/1916
import heapq, sys
def init():
ipt = sys.stdin.readline
n, m = map(int, ipt().split())
adj_list = [[] for _ in range(n+1)]
for _ in range(m):
s, e, c = map(int, ipt().split())
adj_list[s].append((e, c))
adj_list[e].append((s, c))
return n, adj_list
def dijkstra():
dist = [float('inf')] * (n+1)
dist[1] = 0
q = [(0, 1)]
while q:
cd, cn = heapq.heappop(q)
for nn, cost in adj_list[cn]:
nd = cd + cost
if dist[nn] > nd:
dist[nn] = nd
heapq.heappush(q, (nd, nn))
return dist[n]
n, adj_list = init()
print(dijkstra())