結果

問題 No.807 umg tours
ユーザー hedwig100
提出日時 2020-04-23 23:18:03
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 2,311 ms / 4,000 ms
コード長 871 bytes
コンパイル時間 120 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 84,652 KB
最終ジャッジ日時 2024-11-23 20:01:15
合計ジャッジ時間 24,414 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from heapq import heapify,heappush,heappop
input = sys.stdin.readline
INF = 10 ** 16

n,m = map(int,input().split())
G = [[] for _ in range(n)]
for _ in range(m):
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append((b,c))
    G[b].append((a,c))

dist = [INF] * n
dist[0] = 0

q = [(0,0)]
heapify(q)

while q:
    d,v = heappop(q)
    if dist[v] < d:
        continue
    for e,cost in G[v]:
        if dist[e] > dist[v] + cost:
            dist[e] = dist[v] + cost
            heappush(q,(dist[e],e))

dist1 = [INF] * n
dist1[0] = 0
heappush(q,(0,0))
while q:
    d,v = heappop(q)
    if dist1[v] < d:
        continue
    for e,cost in G[v]:
        m = min(dist[v],dist1[v] + cost)
        if dist1[e] > m:
            dist1[e] = m
            heappush(q,(dist1[e],e))

print('\n'.join(map(str,[dist[i] + dist1[i] for i in range(n)])))
0