結果

問題 No.807 umg tours
ユーザー AEnAEn
提出日時 2022-05-18 10:18:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,201 ms / 4,000 ms
コード長 913 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 86,992 KB
実行使用メモリ 153,876 KB
最終ジャッジ日時 2023-10-14 16:49:13
合計ジャッジ時間 22,476 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
75,828 KB
testcase_01 AC 86 ms
75,552 KB
testcase_02 AC 93 ms
75,652 KB
testcase_03 AC 91 ms
75,652 KB
testcase_04 AC 85 ms
75,860 KB
testcase_05 AC 85 ms
75,644 KB
testcase_06 AC 93 ms
76,012 KB
testcase_07 AC 87 ms
75,816 KB
testcase_08 AC 73 ms
71,176 KB
testcase_09 AC 75 ms
71,216 KB
testcase_10 AC 75 ms
71,116 KB
testcase_11 AC 993 ms
115,200 KB
testcase_12 AC 1,260 ms
121,536 KB
testcase_13 AC 1,536 ms
133,100 KB
testcase_14 AC 755 ms
103,204 KB
testcase_15 AC 521 ms
97,420 KB
testcase_16 AC 1,525 ms
134,048 KB
testcase_17 AC 2,115 ms
148,608 KB
testcase_18 AC 2,026 ms
147,376 KB
testcase_19 AC 1,877 ms
144,112 KB
testcase_20 AC 1,032 ms
124,108 KB
testcase_21 AC 1,102 ms
125,940 KB
testcase_22 AC 520 ms
98,756 KB
testcase_23 AC 447 ms
93,188 KB
testcase_24 AC 1,101 ms
147,564 KB
testcase_25 AC 2,201 ms
153,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush, heappop
INF = float('inf')

N, M = map(int, input().split())
adj = [[] for _ in range(N)]
for i in range(M):
    s, t, d = map(int, input().split())
    s -= 1
    t -= 1
    adj[s].append((t, d))
    adj[t].append((s, d))

def dijkstra(s, n):
    dist = [[INF] * 2 for _ in range(N)]
    hq = [(0, 0, s)]
    dist[s][0] = 0
    dist[s][1] = 0
    while hq:
        d, ticket, v = heappop(hq)
        if d > dist[v][ticket]:
            continue
        for to, cost in adj[v]:
            if dist[v][ticket] + cost < dist[to][ticket]:
                dist[to][ticket] = d + cost
                # prev[to] = v
                heappush(hq, (dist[to][ticket], ticket, to))
            if ticket == 0:
                if dist[to][1] > d:
                    dist[to][1] = d
                    heappush(hq, (d, 1, to))
    return dist
dis = dijkstra(0, N)
for a, b in dis:
    print(a+b)
0