結果

問題 No.1995 CHIKA Road
ユーザー titan23titan23
提出日時 2022-07-01 23:19:25
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,102 bytes
コンパイル時間 434 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 192,304 KB
最終ジャッジ日時 2023-08-17 11:03:46
合計ジャッジ時間 19,564 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,460 KB
testcase_01 AC 95 ms
71,612 KB
testcase_02 AC 92 ms
71,720 KB
testcase_03 AC 93 ms
71,564 KB
testcase_04 AC 96 ms
71,740 KB
testcase_05 WA -
testcase_06 AC 249 ms
86,084 KB
testcase_07 AC 371 ms
104,288 KB
testcase_08 AC 108 ms
77,060 KB
testcase_09 AC 107 ms
76,516 KB
testcase_10 AC 453 ms
97,300 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 272 ms
90,920 KB
testcase_17 AC 620 ms
132,940 KB
testcase_18 AC 911 ms
169,156 KB
testcase_19 AC 929 ms
169,516 KB
testcase_20 AC 551 ms
128,704 KB
testcase_21 AC 527 ms
125,012 KB
testcase_22 AC 863 ms
163,932 KB
testcase_23 AC 384 ms
104,292 KB
testcase_24 AC 758 ms
152,168 KB
testcase_25 AC 298 ms
92,972 KB
testcase_26 AC 428 ms
110,952 KB
testcase_27 AC 750 ms
150,168 KB
testcase_28 AC 513 ms
124,472 KB
testcase_29 AC 864 ms
167,692 KB
testcase_30 AC 279 ms
92,940 KB
testcase_31 AC 211 ms
84,856 KB
testcase_32 AC 314 ms
97,824 KB
testcase_33 AC 732 ms
150,168 KB
testcase_34 AC 234 ms
86,640 KB
testcase_35 AC 396 ms
105,308 KB
testcase_36 AC 445 ms
113,280 KB
testcase_37 AC 829 ms
158,932 KB
testcase_38 AC 638 ms
135,360 KB
testcase_39 AC 209 ms
84,296 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]
  if now == nxt: continue
  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