結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-06-02 13:57:13 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 1,247 ms / 5,000 ms |
| コード長 | 911 bytes |
| コンパイル時間 | 125 ms |
| コンパイル使用メモリ | 12,928 KB |
| 実行使用メモリ | 22,016 KB |
| 最終ジャッジ日時 | 2024-07-06 13:38:18 |
| 合計ジャッジ時間 | 3,753 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
import heapq
def read_data():
N, M, S, G = map(int, input().split())
Es = [dict() for i in range(N)]
for m in range(M):
a, b, c = map(int, input().split())
Es[a][b] = c
Es[b][a] = c
return N, M, S, G, Es
def solve(N, M, start, goal, Es):
dist = [float('inf')] * N
path = [tuple() for n in range(N)]
dist[start] = 0
path[start] = (start, )
pq = [(0, path[start])]
while pq:
d, pathi = heapq.heappop(pq)
v = pathi[-1]
if v == goal:
return pathi
for u, nd in Es[v].items():
new_d = d + nd
if (new_d < dist[u]) or (new_d == dist[u] and pathi + (u, ) < path[u]):
dist[u] = new_d
path[u] = pathi + (u, )
heapq.heappush(pq, (new_d, path[u]))
if __name__ == '__main__':
param = read_data()
path = solve(*param)
print(*path)