test/3G.py
2026-07-01 19:46:51 +03:00

31 lines
No EOL
799 B
Python

import sys
import heapq
data = sys.stdin.read().split()
if data:
n = int(data[0])
m = int(data[1])
graph = [[] for _ in range(n + 1)]
idx = 2
for _ in range(m):
u = int(data[idx])
v = int(data[idx+1])
w = int(data[idx+2])
graph[u].append((v, w))
graph[v].append((u, w))
idx += 3
inf = 10**18
dist = [inf] * (n + 1)
dist[1] = 0
pq = [(0, 1)]
while len(pq) > 0:
d, u = heapq.heappop(pq)
if d > dist[u]:
continue
for v, w in graph[u]:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
heapq.heappush(pq, (dist[v], v))
ans = []
for i in range(1, n + 1):
ans.append(str(dist[i]))
sys.stdout.write(" ".join(ans) + "\n")