結果

問題 No.1995 CHIKA Road
ユーザー ThetaTheta
提出日時 2024-04-16 11:29:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,055 ms / 2,000 ms
コード長 1,034 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 195,256 KB
最終ジャッジ日時 2024-04-16 11:29:50
合計ジャッジ時間 17,260 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,760 KB
testcase_01 AC 40 ms
54,016 KB
testcase_02 AC 40 ms
54,528 KB
testcase_03 AC 40 ms
54,528 KB
testcase_04 AC 43 ms
54,656 KB
testcase_05 AC 54 ms
63,360 KB
testcase_06 AC 163 ms
83,720 KB
testcase_07 AC 274 ms
98,292 KB
testcase_08 AC 57 ms
66,048 KB
testcase_09 AC 52 ms
64,640 KB
testcase_10 AC 322 ms
87,936 KB
testcase_11 AC 1,055 ms
195,256 KB
testcase_12 AC 475 ms
133,804 KB
testcase_13 AC 390 ms
118,952 KB
testcase_14 AC 422 ms
120,740 KB
testcase_15 AC 901 ms
185,284 KB
testcase_16 AC 196 ms
88,892 KB
testcase_17 AC 497 ms
126,304 KB
testcase_18 AC 738 ms
152,216 KB
testcase_19 AC 774 ms
153,064 KB
testcase_20 AC 442 ms
120,284 KB
testcase_21 AC 447 ms
114,628 KB
testcase_22 AC 692 ms
142,968 KB
testcase_23 AC 293 ms
99,580 KB
testcase_24 AC 652 ms
140,100 KB
testcase_25 AC 222 ms
91,008 KB
testcase_26 AC 335 ms
105,628 KB
testcase_27 AC 629 ms
138,080 KB
testcase_28 AC 405 ms
114,500 KB
testcase_29 AC 732 ms
150,392 KB
testcase_30 AC 206 ms
90,716 KB
testcase_31 AC 183 ms
83,552 KB
testcase_32 AC 237 ms
92,404 KB
testcase_33 AC 609 ms
136,760 KB
testcase_34 AC 187 ms
84,576 KB
testcase_35 AC 313 ms
102,364 KB
testcase_36 AC 374 ms
109,640 KB
testcase_37 AC 657 ms
142,864 KB
testcase_38 AC 492 ms
121,660 KB
testcase_39 AC 188 ms
82,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from heapq import heappop, heappush
from itertools import pairwise
from math import inf


def main():
    N, M = map(int, input().split())
    graph = defaultdict(dict)
    roads_edge = set((0, N - 1))
    for _ in range(M):
        A, B = map(int, input().split())
        roads_edge.add(A - 1)
        roads_edge.add(B - 1)
        graph[A - 1][B - 1] = 2 * (B - A) - 1

    roads_edge = sorted(roads_edge)
    for prev_n, cur_n in pairwise(roads_edge):
        graph[prev_n][cur_n] = min(
            2 * (cur_n - prev_n), graph[prev_n].get(cur_n, inf))

    distance = {edge: inf for edge in roads_edge}
    distance[0] = 0
    queue = [(0, 0)]
    while queue:
        c_d, c_n = heappop(queue)
        if distance[c_n] < c_d:
            continue
        for n_n, n_d in graph[c_n].items():
            if distance[n_n] > n_d + c_d:
                distance[n_n] = n_d + c_d
                heappush(queue, (n_d + c_d, n_n))
    print(distance[N - 1])


if __name__ == "__main__":
    main()
0