結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
![]() |
提出日時 | 2025-04-15 22:39:26 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,383 ms / 5,000 ms |
コード長 | 1,117 bytes |
コンパイル時間 | 199 ms |
コンパイル使用メモリ | 82,020 KB |
実行使用メモリ | 91,268 KB |
最終ジャッジ日時 | 2025-04-15 22:41:14 |
合計ジャッジ時間 | 4,877 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
import heapq def main(): n, m, S, G = map(int, input().split()) adj = [[] for _ in range(n)] for _ in range(m): a, b, c = map(int, input().split()) adj[a].append((b, c)) adj[b].append((a, c)) dist = [float('inf')] * n path = [tuple() for _ in range(n)] dist[S] = 0 path[S] = (S,) heap = [] heapq.heappush(heap, (0, path[S])) while heap: current_cost, current_path = heapq.heappop(heap) u = current_path[-1] if u == G: print(' '.join(map(str, current_path))) return if current_cost > dist[u]: continue for v, c in adj[u]: new_cost = current_cost + c new_path = current_path + (v,) if new_cost < dist[v]: dist[v] = new_cost path[v] = new_path heapq.heappush(heap, (new_cost, new_path)) elif new_cost == dist[v] and new_path < path[v]: path[v] = new_path heapq.heappush(heap, (new_cost, new_path)) if __name__ == "__main__": main()