結果

問題 No.807 umg tours
ユーザー hogeandfuga
提出日時 2019-04-01 22:17:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,160 ms / 4,000 ms
コード長 753 bytes
コンパイル時間 659 ms
コンパイル使用メモリ 82,276 KB
実行使用メモリ 163,732 KB
最終ジャッジ日時 2024-11-23 19:23:16
合計ジャッジ時間 13,619 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush
import sys
input=sys.stdin.readline

n, m = map(int, input().split())
edge = [[] for _ in range(2*n)]
for i in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edge[a].append((c, b))
    edge[b].append((c, a))
    edge[a+n].append((c, b+n))
    edge[b+n].append((c, a+n))
    edge[a].append((0, b+n))
    edge[b].append((0, a+n))

dist = [float("inf")]*(2*n)
dist[0] = dist[n] = 0
pq = []
heappush(pq, (0, 0))
heappush(pq, (0, n))
while pq:
    d, cur = heappop(pq)
    if d > dist[cur]:
        continue

    for cost, to in edge[cur]:
        if dist[to] > d + cost:
            dist[to] = d + cost
            heappush(pq, (d+cost, to))

for i in range(n):
    print(dist[i]+dist[i+n])

0