結果

問題 No.1995 CHIKA Road
コンテスト
ユーザー titan23
提出日時 2022-07-01 23:27:12
言語 PyPy3
(8.0.0 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 599 ms / 2,000 ms
+ 836µs
コード長 895 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 66 ms
コンパイル使用メモリ 81,236 KB
実行使用メモリ 157,344 KB
最終ジャッジ日時 2026-09-30 05:57:24
合計ジャッジ時間 11,825 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
from collections import defaultdict, deque
from heapq import heapify, heappush, heappop
input = lambda: sys.stdin.readline().rstrip()
inf = float('inf')

def dijkstra(G: list, s: int) -> list:
  "Return dist from s. / O(|E|log|V|)"
  dist = defaultdict(lambda: inf)
  dist[s] = 0
  hq = [(0, s)]
  while hq:
    d, v = heappop(hq)
    if dist[v] < d:
      continue
    for x,c in G[v]:
      if dist[x] > d + c:
        dist[x] = d + c
        heappush(hq, (d+c, x))
  return dist

#  -----------------------  #

n, m = map(int, input().split())
G = defaultdict(list)
E = []

for _ in range(m):
  a, b = map(int, input().split())
  G[a-1].append((b-1, 2*b-2*a-1))
  E.append(a-1)
  E.append(b-1)

E.append(0)
E.append(n-1)
E.sort()

for i in range(len(E)-1):
  now, nxt = E[i], E[i+1]
  dist = (E[i+1] - E[i]) * 2
  G[now].append((nxt, dist))

dist = dijkstra(G, 0)
print(dist[n-1])
0