結果

問題 No.1868 Teleporting Cyanmond
ユーザー qib
提出日時 2023-03-13 03:20:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 260 ms / 2,000 ms
コード長 522 bytes
コンパイル時間 370 ms
コンパイル使用メモリ 81,800 KB
実行使用メモリ 98,164 KB
最終ジャッジ日時 2024-09-18 07:20:29
合計ジャッジ時間 6,529 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

INF = 1 << 60

n = int(input())
r = list(map(int, input().split()))
g = [[] for _ in range(n)]
for u in range(n - 1):
  g[u].append((r[u] - 1, 1))

for u in range(n - 1):
  g[u + 1].append((u, 0))

src = 0
dist = [INF for _ in range(n)]
dist[src] = 0
hp = [(0, src)]
while len(hp) > 0:
  cd, cur = heapq.heappop(hp)
  if cd > dist[cur]:
    continue
  for nxt, cost in g[cur]:
    if dist[nxt] > dist[cur] + cost:
      dist[nxt] = dist[cur] + cost
      heapq.heappush(hp, (dist[nxt], nxt))

print(dist[-1])
0