結果

問題 No.1995 CHIKA Road
ユーザー tktk_snsntktk_snsn
提出日時 2022-07-02 00:59:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 811 ms / 2,000 ms
コード長 899 bytes
コンパイル時間 383 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 167,000 KB
最終ジャッジ日時 2023-08-17 12:47:23
合計ジャッジ時間 15,986 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,352 KB
testcase_01 AC 73 ms
71,316 KB
testcase_02 AC 72 ms
71,372 KB
testcase_03 AC 71 ms
71,436 KB
testcase_04 AC 73 ms
71,352 KB
testcase_05 AC 80 ms
75,616 KB
testcase_06 AC 172 ms
83,772 KB
testcase_07 AC 249 ms
94,288 KB
testcase_08 AC 95 ms
76,700 KB
testcase_09 AC 82 ms
75,964 KB
testcase_10 AC 296 ms
101,588 KB
testcase_11 AC 811 ms
166,664 KB
testcase_12 AC 326 ms
128,224 KB
testcase_13 AC 296 ms
117,284 KB
testcase_14 AC 308 ms
117,764 KB
testcase_15 AC 693 ms
167,000 KB
testcase_16 AC 196 ms
87,924 KB
testcase_17 AC 382 ms
115,508 KB
testcase_18 AC 556 ms
138,332 KB
testcase_19 AC 582 ms
138,148 KB
testcase_20 AC 368 ms
109,712 KB
testcase_21 AC 352 ms
110,128 KB
testcase_22 AC 542 ms
138,356 KB
testcase_23 AC 248 ms
93,444 KB
testcase_24 AC 475 ms
121,472 KB
testcase_25 AC 203 ms
89,740 KB
testcase_26 AC 282 ms
100,612 KB
testcase_27 AC 458 ms
122,692 KB
testcase_28 AC 345 ms
110,068 KB
testcase_29 AC 550 ms
140,440 KB
testcase_30 AC 187 ms
89,472 KB
testcase_31 AC 155 ms
82,592 KB
testcase_32 AC 212 ms
92,996 KB
testcase_33 AC 453 ms
123,052 KB
testcase_34 AC 159 ms
84,004 KB
testcase_35 AC 261 ms
97,980 KB
testcase_36 AC 295 ms
107,968 KB
testcase_37 AC 531 ms
139,428 KB
testcase_38 AC 414 ms
124,496 KB
testcase_39 AC 160 ms
82,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 18

N, M = map(int, input().split())
AB = list(list(map(int, input().split())) for _ in range(M))

exist = {1, N}
for a, b in AB:
    exist.add(a)
    exist.add(b)

exist = sorted(exist)
etoi = {e: i for i, e in enumerate(exist)}
size = len(exist)

G = [[] for _ in range(size)]
for i in range(size - 1):
    d = (exist[i+1] - exist[i]) * 2
    G[i].append((i + 1, d))
    G[i + 1].append((i, d))
for a, b in AB:
    d = b + b - a - a - 1
    a = etoi[a]
    b = etoi[b]
    G[a].append((b, d))
    G[b].append((a, d))


dist = [inf] * size
dist[0] = 0
que = [(0, 0)]
while que:
    ds, s = heapq.heappop(que)
    if dist[s] < ds:
        continue
    for t, dt in G[s]:
        if dist[t] > ds + dt:
            dist[t] = ds + dt
            heapq.heappush(que, (ds + dt, t))

print(dist[size-1])
0