結果

問題 No.160 最短経路のうち辞書順最小
ユーザー lloyzlloyz
提出日時 2023-06-27 21:40:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 161 ms / 5,000 ms
コード長 782 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 79,424 KB
最終ジャッジ日時 2023-09-17 19:57:34
合計ジャッジ時間 5,317 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,260 KB
testcase_01 AC 96 ms
71,672 KB
testcase_02 AC 95 ms
71,748 KB
testcase_03 AC 97 ms
71,704 KB
testcase_04 AC 141 ms
78,016 KB
testcase_05 AC 155 ms
78,800 KB
testcase_06 AC 161 ms
79,424 KB
testcase_07 AC 135 ms
77,888 KB
testcase_08 AC 133 ms
77,968 KB
testcase_09 AC 135 ms
77,904 KB
testcase_10 AC 133 ms
77,924 KB
testcase_11 AC 134 ms
77,752 KB
testcase_12 AC 138 ms
77,820 KB
testcase_13 AC 135 ms
78,144 KB
testcase_14 AC 133 ms
78,088 KB
testcase_15 AC 131 ms
77,832 KB
testcase_16 AC 133 ms
77,904 KB
testcase_17 AC 131 ms
78,112 KB
testcase_18 AC 131 ms
77,624 KB
testcase_19 AC 132 ms
77,868 KB
testcase_20 AC 133 ms
77,920 KB
testcase_21 AC 132 ms
78,060 KB
testcase_22 AC 130 ms
77,904 KB
testcase_23 AC 135 ms
77,888 KB
testcase_24 AC 135 ms
77,900 KB
testcase_25 AC 131 ms
77,804 KB
testcase_26 AC 132 ms
77,808 KB
testcase_27 AC 104 ms
72,228 KB
testcase_28 AC 146 ms
79,180 KB
testcase_29 AC 103 ms
72,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque
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)]
From = [-1 for _ in range(n)]
DP[g] = 0
H = [(0, g)]
while H:
    cc, cp = heappop(H)
    if cc > DP[cp]:
        continue
    for np, dc in G[cp]:
        nc = cc + dc
        if nc < DP[np]:
            DP[np] = nc
            From[np] = cp
            heappush(H, (nc, np))
        elif nc == DP[np]:
            From[np] = min(From[np], cp)

ANS = [s]
while ANS[-1] != g:
    cp = From[ANS[-1]]
    ANS.append(cp)
print(*ANS)
0