結果

問題 No.1995 CHIKA Road
ユーザー titan23titan23
提出日時 2022-07-01 23:15:24
言語 PyPy3
(7.3.13)
結果
WA  
実行時間 -
コード長 1,076 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 87,056 KB
実行使用メモリ 192,048 KB
最終ジャッジ日時 2023-08-17 10:58:38
合計ジャッジ時間 19,440 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,452 KB
testcase_01 AC 90 ms
71,548 KB
testcase_02 AC 92 ms
71,496 KB
testcase_03 AC 93 ms
71,656 KB
testcase_04 AC 92 ms
71,440 KB
testcase_05 WA -
testcase_06 AC 232 ms
86,256 KB
testcase_07 AC 365 ms
103,208 KB
testcase_08 AC 110 ms
77,012 KB
testcase_09 AC 107 ms
76,540 KB
testcase_10 AC 470 ms
97,328 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 269 ms
90,972 KB
testcase_17 AC 611 ms
133,264 KB
testcase_18 AC 919 ms
169,896 KB
testcase_19 AC 920 ms
171,432 KB
testcase_20 AC 564 ms
129,396 KB
testcase_21 AC 526 ms
124,044 KB
testcase_22 AC 876 ms
164,496 KB
testcase_23 AC 377 ms
104,264 KB
testcase_24 AC 783 ms
152,924 KB
testcase_25 AC 287 ms
92,732 KB
testcase_26 AC 425 ms
110,928 KB
testcase_27 AC 758 ms
150,832 KB
testcase_28 AC 528 ms
124,868 KB
testcase_29 AC 911 ms
167,664 KB
testcase_30 AC 276 ms
92,572 KB
testcase_31 AC 213 ms
84,376 KB
testcase_32 AC 311 ms
97,488 KB
testcase_33 AC 740 ms
149,752 KB
testcase_34 AC 240 ms
87,056 KB
testcase_35 AC 389 ms
106,096 KB
testcase_36 AC 447 ms
113,932 KB
testcase_37 AC 832 ms
158,560 KB
testcase_38 AC 650 ms
135,580 KB
testcase_39 AC 210 ms
84,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
st = defaultdict(set)
E = []
mx = 0

for _ in range(m):
  a, b = map(int, input().split())
  G[a-1].append((b-1, 2*b-2*a-1))
  st[a-1].add(b-1)
  E.append(a-1)
  E.append(b-1)
  mx = max(mx, b-1)
if n-1 not in st[mx]:
  G[mx].append((n-1, (n-1 - mx)*2))

E.sort()

for i in range(len(E)-1):
  now, nxt = E[i], E[i+1]
  dist = (E[i+1] - E[i]) * 2
  if nxt not in st[now]:
    st[now].add(nxt)
    G[now].append((nxt, dist))
for k in G.keys():
  G[k].sort()

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