結果
問題 | No.160 最短経路のうち辞書順最小 |
ユーザー |
|
提出日時 | 2022-06-25 12:21:22 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 95 ms / 5,000 ms |
コード長 | 844 bytes |
コンパイル時間 | 295 ms |
コンパイル使用メモリ | 82,460 KB |
実行使用メモリ | 77,396 KB |
最終ジャッジ日時 | 2024-11-14 08:33:40 |
合計ジャッジ時間 | 3,483 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
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)