結果

問題 No.160 最短経路のうち辞書順最小
ユーザー tnodino
提出日時 2022-07-24 19:47:39
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 128 ms / 5,000 ms
コード長 651 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2024-07-06 14:45:17
合計ジャッジ時間 2,498 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
INF = 1<<64
N,M,S,T = map(int,input().split())
G = [[] for _ in range(N)]
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()
cost = [INF] * N
cost[T] = 0
Queue = deque()
Queue.append(T)
while Queue:
    pos = Queue.popleft()
    for nxt,c in G[pos]:
        if cost[pos] + c < cost[nxt]:
            cost[nxt] = cost[pos] + c
            Queue.append(nxt)
pos = S
ans = [S]
while pos != T:
    for nxt,c in G[pos]:
        if cost[pos] - c == cost[nxt]:
            ans.append(nxt)
            pos = nxt
            break
print(*ans)
0