結果

問題 No.160 最短経路のうち辞書順最小
ユーザー lloyzlloyz
提出日時 2023-06-27 21:15:19
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 834 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 86,884 KB
実行使用メモリ 78,880 KB
最終ジャッジ日時 2023-09-17 19:28:54
合計ジャッジ時間 5,137 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,612 KB
testcase_01 AC 90 ms
71,424 KB
testcase_02 AC 94 ms
71,540 KB
testcase_03 AC 91 ms
71,160 KB
testcase_04 AC 127 ms
77,728 KB
testcase_05 AC 136 ms
77,572 KB
testcase_06 AC 143 ms
78,668 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 131 ms
77,896 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 128 ms
77,524 KB
testcase_20 AC 123 ms
77,896 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 98 ms
71,796 KB
testcase_28 WA -
testcase_29 AC 96 ms
71,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from heapq import heapify, heappop, heappush

n, m, s, g = map(int, input().split())
G = defaultdict(list)
for _ in range(m):
    a, b, c = map(int, input().split())
    G[a].append((b, c))
    G[b].append((a, c))
for i in range(n):
    G[i].sort(key=lambda x: x[0])
INF = 10**18
DP = [INF for _ in range(n)]
DP[s] = 0
H = [(0, s)]
while H:
    cc, cp = heappop(H)
    if cc > DP[cp]:
        continue
    if cp == g:
        break
    for np, dc in G[cp]:
        nc = cc + dc
        if nc >= DP[np]:
            continue
        DP[np] = nc
        heappush(H, (nc, np))
ANS = [g]
res = DP[g]
while ANS[-1] != s:
    cp = ANS[-1]
    for np, dc in G[cp]:
        nc = res - dc
        if nc == DP[np]:
            ANS.append(np)
            res -= dc
            break
ANS.reverse()
print(*ANS)
0