結果
| 問題 |
No.807 umg tours
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 17:04:09 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 2,829 ms / 4,000 ms |
| コード長 | 1,399 bytes |
| コンパイル時間 | 234 ms |
| コンパイル使用メモリ | 82,396 KB |
| 実行使用メモリ | 182,696 KB |
| 最終ジャッジ日時 | 2025-06-12 17:04:38 |
| 合計ジャッジ時間 | 26,257 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
import sys
import heapq
def main():
input = sys.stdin.read
data = input().split()
idx = 0
N = int(data[idx])
idx += 1
M = int(data[idx])
idx += 1
adj = [[] for _ in range(N+1)]
for _ in range(M):
a = int(data[idx])
idx += 1
b = int(data[idx])
idx += 1
c = int(data[idx])
idx += 1
adj[a].append((b, c))
adj[b].append((a, c))
INF = float('inf')
dist = [[INF] * 2 for _ in range(N+1)]
dist[1][0] = 0
heap = []
heapq.heappush(heap, (0, 1, 0))
while heap:
d, u, s = heapq.heappop(heap)
if d > dist[u][s]:
continue
for v, c in adj[u]:
if s == 0:
if dist[v][0] > d + c:
dist[v][0] = d + c
heapq.heappush(heap, (dist[v][0], v, 0))
if dist[v][1] > d:
dist[v][1] = d
heapq.heappush(heap, (dist[v][1], v, 1))
else:
if dist[v][1] > d + c:
dist[v][1] = d + c
heapq.heappush(heap, (dist[v][1], v, 1))
for i in range(1, N+1):
if i == 1:
print(0)
continue
d0 = dist[i][0]
d1 = dist[i][1]
min_dist = min(2 * d0, d0 + d1)
print(min_dist)
if __name__ == "__main__":
main()
gew1fw