結果

問題 No.807 umg tours
ユーザー stngstng
提出日時 2022-07-02 18:50:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,892 ms / 4,000 ms
コード長 1,566 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 86,672 KB
実行使用メモリ 181,284 KB
最終ジャッジ日時 2023-08-18 10:32:43
合計ジャッジ時間 29,603 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
76,080 KB
testcase_01 AC 85 ms
76,096 KB
testcase_02 AC 96 ms
76,120 KB
testcase_03 AC 93 ms
75,980 KB
testcase_04 AC 81 ms
76,020 KB
testcase_05 AC 82 ms
76,016 KB
testcase_06 AC 96 ms
76,052 KB
testcase_07 AC 91 ms
75,892 KB
testcase_08 AC 70 ms
71,360 KB
testcase_09 AC 73 ms
71,352 KB
testcase_10 AC 72 ms
71,292 KB
testcase_11 AC 1,412 ms
136,100 KB
testcase_12 AC 1,798 ms
136,248 KB
testcase_13 AC 2,239 ms
152,388 KB
testcase_14 AC 1,095 ms
113,140 KB
testcase_15 AC 795 ms
105,768 KB
testcase_16 AC 2,150 ms
156,884 KB
testcase_17 AC 2,892 ms
175,812 KB
testcase_18 AC 2,723 ms
173,808 KB
testcase_19 AC 2,429 ms
169,048 KB
testcase_20 AC 1,195 ms
127,868 KB
testcase_21 AC 1,288 ms
131,664 KB
testcase_22 AC 631 ms
99,716 KB
testcase_23 AC 708 ms
96,544 KB
testcase_24 AC 1,328 ms
172,096 KB
testcase_25 AC 2,778 ms
181,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush
from sys import stdin

n,m = map(int,input().split())

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

INF = 10**14

dp = [[INF]*2 for i in range(n)]
dp[0][0] = 0
dp[0][1] = 0

q = [(0, 0, 0)]#c, idx, use
while q:
    c, j, use = heappop(q)
    abc = abcs[j]
    if dp[j][use] != c:
        continue
    for k in range(len(abc)):
        j1,c1 = abc[k]
        ndis = c1 + c
        if use == 0:
            if dp[j1][0] > ndis:
                dp[j1][0] = ndis
                heappush(q, (ndis, j1, 0))
            if dp[j1][1] > c:
                dp[j1][1] = c
                heappush(q, (c, j1, 1))
        else:
            if dp[j1][1] > ndis:
                dp[j1][1] = ndis
                heappush(q, (ndis, j1, 1))

def dijkstra(s, n): # (始点, ノード数)
    dist = [INF] * n
    hq = [(0, s)] # (distance, node)
    dist[s] = 0
    seen = [False] * n # ノードが確定済みかどうか
    while hq:
        tmp = heappop(hq) # ノードを pop する
        v = tmp[1]
        c = tmp[0]
        seen[v] = True
        if dist[v] != c:
            continue
        for to, cost in abcs[v]: # ノード v に隣接しているノードに対して
            if seen[to] == False and dist[v] + cost < dist[to]:
                dist[to] = dist[v] + cost
                heappush(hq, (dist[to], to))
    return dist

fst = dijkstra(0,n)
print(0)
for i in range(1,n):
    print(fst[i]+dp[i][1])
0