結果
| 問題 |
No.807 umg tours
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 21:38:07 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,964 ms / 4,000 ms |
| コード長 | 1,831 bytes |
| コンパイル時間 | 140 ms |
| コンパイル使用メモリ | 82,244 KB |
| 実行使用メモリ | 173,020 KB |
| 最終ジャッジ日時 | 2025-06-12 21:42:12 |
| 合計ジャッジ時間 | 18,330 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
import sys
import heapq
def main():
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr])
ptr += 1
M = int(input[ptr])
ptr += 1
adj = [[] for _ in range(N + 1)]
for _ in range(M):
a = int(input[ptr])
ptr += 1
b = int(input[ptr])
ptr += 1
c = int(input[ptr])
ptr += 1
adj[a].append((b, c))
adj[b].append((a, c))
# Initialize distances
INF = float('inf')
d0 = [INF] * (N + 1)
d0[1] = 0
d1 = [INF] * (N + 1)
heap = []
heapq.heappush(heap, (0, 1, 0)) # (distance, node, state)
while heap:
dist, u, s = heapq.heappop(heap)
if s == 0 and dist > d0[u]:
continue
if s == 1 and dist > d1[u]:
continue
for v, c in adj[u]:
if s == 0:
# Not using ticket
new_dist = dist + c
if new_dist < d0[v]:
d0[v] = new_dist
heapq.heappush(heap, (new_dist, v, 0))
# Using ticket
new_dist = dist + 0
if new_dist < d1[v]:
d1[v] = new_dist
heapq.heappush(heap, (new_dist, v, 1))
else:
# Using ticket already
new_dist = dist + c
if new_dist < d1[v]:
d1[v] = new_dist
heapq.heappush(heap, (new_dist, v, 1))
for i in range(1, N + 1):
if d0[i] == INF:
print(0)
else:
option1 = 2 * d0[i]
option2 = d1[i] + d0[i] if d1[i] != INF else INF
option3 = d0[i] + d1[i] if d1[i] != INF else INF
min_option = min(option1, option2, option3)
print(min_option)
if __name__ == '__main__':
main()
gew1fw