結果

問題 No.160 最短経路のうち辞書順最小
ユーザー ああいい
提出日時 2022-03-24 23:18:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 98 ms / 5,000 ms
コード長 665 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 77,312 KB
最終ジャッジ日時 2024-10-13 06:59:29
合計ジャッジ時間 3,746 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M,S,GG = 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))
import heapq
inf = 10 ** 8
dist = [inf] * N
dist[GG] = 0
q = [(0,GG)]
while q:
    d,now = heapq.heappop(q)
    if dist[now] < d:continue
    for v,c in G[now]:
        if dist[v] > d + c:
            dist[v] = d + c
            heapq.heappush(q,(d+c,v))
#print(dist)
ans = []
now = S
while now != GG:
    ans.append(now)
    _min = N + 5
    for v,c in G[now]:
        if dist[v] == dist[now] - c:
            if v < _min:
                _min = v
    now = _min
ans.append(GG)
print(*ans)
        
0