結果

問題 No.807 umg tours
ユーザー ああいい
提出日時 2021-12-29 18:40:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,999 ms / 4,000 ms
コード長 1,130 bytes
コンパイル時間 203 ms
コンパイル使用メモリ 82,252 KB
実行使用メモリ 144,788 KB
最終ジャッジ日時 2024-10-04 06:45:24
合計ジャッジ時間 20,407 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M = map(int,input().split())
G = [[] for _ in range(N + 1)]
for _ in range(M):
    a,b,c = map(int,input().split())
    G[a].append((b,c))
    G[b].append((a,c))

import heapq
C = 10 ** 16
dist = [C] * (N + 1)
dist[1] = 0
q = []
heapq.heapify(q)
heapq.heappush(q,(0,1))
while len(q):
    d,index = heapq.heappop(q)
    if d > dist[index]:
        continue
    for v,c in G[index]:
        if dist[v] > d + c:
            dist[v] = d + c
            heapq.heappush(q,(d+c,v))
dist2 = [C] * (N + 1)
dist2[1] = 0
heapq.heappush(q,(0,1,False))
while len(q):
    d,index,flag = heapq.heappop(q)
    if flag and d > dist2[index]:
        continue
    for v,c in G[index]:
        if flag:
            if dist2[v] > dist2[index] + c:
                dist2[v] = dist2[index] + c
                heapq.heappush(q,(d + c,v,True))
        else:
            if dist2[v] > dist[index]:
                dist2[v] = dist[index]
                heapq.heappush(q,(dist[index],v,True))
            if dist[v] >= dist[index] + c:
                heapq.heappush(q,(dist[index] + c,v,False))
for i in range(N):
    print(dist[i+1] + dist2[i+1])
    
0