結果

問題 No.807 umg tours
ユーザー rlangevinrlangevin
提出日時 2023-02-11 12:45:12
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,358 bytes
コンパイル時間 446 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 113,248 KB
最終ジャッジ日時 2023-09-22 10:44:55
合計ジャッジ時間 12,891 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 81 ms
71,488 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 78 ms
71,384 KB
testcase_05 WA -
testcase_06 AC 94 ms
76,352 KB
testcase_07 WA -
testcase_08 AC 73 ms
71,548 KB
testcase_09 AC 75 ms
71,616 KB
testcase_10 AC 76 ms
71,348 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 642 ms
106,404 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 439 ms
102,524 KB
testcase_21 AC 492 ms
106,736 KB
testcase_22 AC 285 ms
89,784 KB
testcase_23 AC 281 ms
87,892 KB
testcase_24 AC 386 ms
109,060 KB
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

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