結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-31 17:25:47 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,216 bytes |
| コンパイル時間 | 221 ms |
| コンパイル使用メモリ | 82,388 KB |
| 実行使用メモリ | 77,660 KB |
| 最終ジャッジ日時 | 2025-03-31 17:26:57 |
| 合計ジャッジ時間 | 3,472 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 8 WA * 18 |
ソースコード
import heapq
def main():
N, M, S, G = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(M):
a, b, c = map(int, input().split())
edges[a].append((b, c))
edges[b].append((a, c))
INF = float('inf')
dist = [INF] * N
prev = [-1] * N
dist[S] = 0
heap = []
heapq.heappush(heap, (0, S))
while heap:
current_dist, u = heapq.heappop(heap)
if current_dist > dist[u]:
continue
for v, c in edges[u]:
new_dist = current_dist + c
if new_dist < dist[v]:
dist[v] = new_dist
prev[v] = u
heapq.heappush(heap, (new_dist, v))
elif new_dist == dist[v]:
if prev[v] == -1 or u < prev[v]:
prev[v] = u
heapq.heappush(heap, (new_dist, v))
path = []
current = G
while current != S:
path.append(current)
current = prev[current]
if current == -1:
break # This case won't occur as per problem constraints
path.append(S)
path = path[::-1]
print(' '.join(map(str, path)))
if __name__ == "__main__":
main()
lam6er