結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,828 KB
testcase_01 AC 98 ms
71,372 KB
testcase_02 AC 96 ms
71,592 KB
testcase_03 AC 98 ms
71,808 KB
testcase_04 AC 95 ms
71,828 KB
testcase_05 WA -
testcase_06 AC 243 ms
85,508 KB
testcase_07 AC 389 ms
104,268 KB
testcase_08 AC 111 ms
77,096 KB
testcase_09 AC 108 ms
76,764 KB
testcase_10 AC 565 ms
116,120 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 265 ms
90,880 KB
testcase_17 AC 634 ms
134,056 KB
testcase_18 AC 931 ms
172,948 KB
testcase_19 AC 964 ms
173,436 KB
testcase_20 AC 593 ms
128,984 KB
testcase_21 AC 555 ms
124,528 KB
testcase_22 AC 895 ms
164,192 KB
testcase_23 AC 378 ms
103,460 KB
testcase_24 AC 803 ms
155,316 KB
testcase_25 AC 299 ms
93,160 KB
testcase_26 AC 442 ms
111,796 KB
testcase_27 AC 768 ms
152,160 KB
testcase_28 AC 566 ms
124,692 KB
testcase_29 AC 922 ms
167,336 KB
testcase_30 AC 280 ms
92,992 KB
testcase_31 AC 230 ms
84,800 KB
testcase_32 AC 324 ms
98,436 KB
testcase_33 AC 763 ms
151,112 KB
testcase_34 AC 250 ms
86,872 KB
testcase_35 AC 410 ms
106,312 KB
testcase_36 AC 455 ms
113,420 KB
testcase_37 AC 863 ms
162,532 KB
testcase_38 AC 660 ms
139,120 KB
testcase_39 AC 228 ms
84,304 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)
L = 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)
  L.add((a-1, b-1))
  E.append(a-1)
  E.append(b-1)
  mx = max(mx, b-1)
if (mx, n-1) not in L:
  L.add((mx, n-1))
  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 (now, nxt) not in st[now]:
    G[now].append((nxt, dist))
for k in G.keys():
  G[k].sort()

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