結果

問題 No.160 最短経路のうち辞書順最小
ユーザー 👑 rin204
提出日時 2022-07-05 16:46:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 104 ms / 5,000 ms
コード長 702 bytes
コンパイル時間 420 ms
コンパイル使用メモリ 82,260 KB
実行使用メモリ 77,540 KB
最終ジャッジ日時 2024-12-15 23:46:24
合計ジャッジ時間 3,840 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *

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

edges = [[] for _ in range(n)]
for _ in range(m):
    u, v, c = map(int, input().split())
    edges[u].append((v, c))
    edges[v].append((u, c))

dist = [1 << 30] * n
dist[g] = 0
nex = [-1] * n
hq = [g]
while hq:
    tmp = heappop(hq)
    d = tmp // n
    pos = tmp - d * n
    if dist[pos] < d:
        continue
    for npos, c in edges[pos]:
        tmp = c + d
        if tmp < dist[npos]:
            dist[npos] = tmp
            heappush(hq, tmp * n + npos)
            nex[npos] = pos
        elif tmp == dist[npos] and pos < nex[npos]:
            nex[npos] = pos
ans = []
while s != -1:
    ans.append(s)
    s = nex[s]
print(*ans)
0