結果

問題 No.1995 CHIKA Road
ユーザー hiragn
提出日時 2022-12-14 12:19:07
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 945 bytes
コンパイル時間 1,490 ms
コンパイル使用メモリ 82,212 KB
実行使用メモリ 846,920 KB
最終ジャッジ日時 2024-11-08 03:44:18
合計ジャッジ時間 3,207 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 RE * 1
other AC * 3 MLE * 1 -- * 33
権限があれば一括ダウンロードができます

ソースコード

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())

    edge = [[] for _ in range(n + 1)]
    for _ in range(m):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append([2 * (b - a) - 1, b])
    for i in range(n - 1):
        edge[i].append([2, i + 1])

    ans = dijkstra(edge, 0)[n - 1]
    print(ans)


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