結果

問題 No.807 umg tours
ユーザー rlangevin
提出日時 2023-02-11 12:45:12
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,358 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 82,440 KB
実行使用メモリ 109,844 KB
最終ジャッジ日時 2024-07-08 02:39:04
合計ジャッジ時間 11,359 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12 WA * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline
from heapq import heappush, heappop
inf = float('inf')


def dijkstra(s, g, N):
    # ゴールがない場合はg=-1とする。

    def cost(v, m):
        return v * N + m

    dist = [inf] * N
    dist2 = [inf] * N
    mindist = [inf] * N
    seen = [False] * N
    Q = [cost(0, s)]
    while Q:
        c, m = divmod(heappop(Q), N)
        if seen[m]:
            continue
        seen[m] = True
        dist[m] = c
        for v, _ in G[m]:
            dist2[v] = min(dist2[v], dist[m])
            # print("test", m, v, dist2[v]) 
        if m == g:
            return dist

        #------heapをアップデートする。--------
        for u, C in G[m]:
            dist2[u] = min(dist2[u], dist2[m] + C)
            if seen[u]:
                continue
            newdist = dist[m] + C

            #------------------------------------
            if newdist >= mindist[u]:
                continue
            mindist[u] = newdist
            heappush(Q, cost(newdist, u))
            
        
    return dist, dist2


N, M = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
    a, b, c, = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append((b, c))
    G[b].append((a, c))
    
D1, D2 = dijkstra(0, -1, N)
for i in range(N):
    print(D1[i] + min(D1[i], D2[i]))
0