結果

問題 No.160 最短経路のうち辞書順最小
ユーザー DrDrpilot
提出日時 2022-06-25 12:21:36
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 133 ms / 5,000 ms
コード長 844 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 14,464 KB
最終ジャッジ日時 2024-11-14 08:33:44
合計ジャッジ時間 2,338 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
n,m,S,G=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))
dst=[10**18]*n
cfm=[False]*n
pre=[None]*n
q=[]
heapq.heappush(q,(0,G,-1))
while q:
    now_d,now_place,pre_place=heapq.heappop(q)
    if cfm[now_place]:
        continue
    pre[now_place]=pre_place
    cfm[now_place]=True
    dst[now_place]=now_d
    for to_place,cost in g[now_place]:
        if cfm[to_place]:
            continue
        if dst[to_place]<=now_d+cost:
            continue
        dst[to_place]=now_d+cost
        heapq.heappush(q,(now_d+cost,to_place,now_place))
now=S;ans=[]
while now!=G:
    tmp=10**18
    ans.append(now)
    for to,cost in g[now]:
        if dst[to]+cost==dst[now]:
            tmp=min(tmp,to)
    now=tmp
ans.append(G)
print(*ans)
0