結果
問題 |
No.1995 CHIKA Road
|
ユーザー |
![]() |
提出日時 | 2022-12-14 18:34:02 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 912 ms / 2,000 ms |
コード長 | 1,198 bytes |
コンパイル時間 | 370 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 160,432 KB |
最終ジャッジ日時 | 2024-11-08 10:21:30 |
合計ジャッジ時間 | 16,661 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
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()