結果

問題 No.1995 CHIKA Road
ユーザー hiragnhiragn
提出日時 2022-12-14 18:34:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 879 ms / 2,000 ms
コード長 1,198 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 160,288 KB
最終ジャッジ日時 2024-04-25 22:15:43
合計ジャッジ時間 15,809 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
67,040 KB
testcase_01 AC 60 ms
66,844 KB
testcase_02 AC 59 ms
67,676 KB
testcase_03 AC 59 ms
68,072 KB
testcase_04 AC 61 ms
68,460 KB
testcase_05 AC 69 ms
71,444 KB
testcase_06 AC 184 ms
85,304 KB
testcase_07 AC 272 ms
96,956 KB
testcase_08 AC 76 ms
74,524 KB
testcase_09 AC 71 ms
72,324 KB
testcase_10 AC 337 ms
98,296 KB
testcase_11 AC 879 ms
160,288 KB
testcase_12 AC 375 ms
124,288 KB
testcase_13 AC 347 ms
112,084 KB
testcase_14 AC 365 ms
111,196 KB
testcase_15 AC 696 ms
148,008 KB
testcase_16 AC 208 ms
89,172 KB
testcase_17 AC 437 ms
117,060 KB
testcase_18 AC 630 ms
132,860 KB
testcase_19 AC 642 ms
133,024 KB
testcase_20 AC 422 ms
109,108 KB
testcase_21 AC 391 ms
107,376 KB
testcase_22 AC 600 ms
130,244 KB
testcase_23 AC 280 ms
98,416 KB
testcase_24 AC 565 ms
124,064 KB
testcase_25 AC 235 ms
89,128 KB
testcase_26 AC 329 ms
103,504 KB
testcase_27 AC 543 ms
123,736 KB
testcase_28 AC 391 ms
107,304 KB
testcase_29 AC 609 ms
132,896 KB
testcase_30 AC 215 ms
88,440 KB
testcase_31 AC 182 ms
84,480 KB
testcase_32 AC 247 ms
91,296 KB
testcase_33 AC 529 ms
122,360 KB
testcase_34 AC 185 ms
85,264 KB
testcase_35 AC 302 ms
100,852 KB
testcase_36 AC 338 ms
104,868 KB
testcase_37 AC 579 ms
128,384 KB
testcase_38 AC 445 ms
115,992 KB
testcase_39 AC 181 ms
83,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heapify, heappop, heappush
from typing import List


def dijkstra(edges: "List[List[(cost, to)]]", start_node: int) -> list:
    hq = []
    heapify(hq)
    dist = [float("inf")] * len(edges)
    heappush(hq, (0, start_node))
    dist[start_node] = 0
    while hq:
        min_cost, now = heappop(hq)
        if min_cost > dist[now]:
            continue
        for cost, nxt in edges[now]:
            if dist[nxt] > dist[now] + cost:
                dist[nxt] = dist[now] + cost
                heappush(hq, (dist[nxt], nxt))
    return dist


def main():
    n, m = map(int, input().split())

    st = {0, n - 1}
    edge = []
    for _ in [0] * m:
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge.append((a, b))
        st.add(a)
        st.add(b)

    lst = sorted(list(st))
    nn = len(lst)
    h = {x: i for i, x in enumerate(lst)}  # 座標圧縮

    route = [[] for _ in [0] * nn]
    for a, b in edge:
        route[h[a]].append([2 * b - 2 * a - 1, h[b]])
    for i in range(nn - 1):
        route[i].append([2 * (lst[i + 1] - lst[i]), i + 1])
    ans = dijkstra(route, 0)[-1]
    print(ans)


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