結果

問題 No.1995 CHIKA Road
ユーザー kept1994kept1994
提出日時 2022-10-30 19:32:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 696 ms / 2,000 ms
コード長 2,043 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 87,104 KB
実行使用メモリ 162,312 KB
最終ジャッジ日時 2023-09-21 17:56:18
合計ジャッジ時間 14,591 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,332 KB
testcase_01 AC 73 ms
71,260 KB
testcase_02 AC 74 ms
71,184 KB
testcase_03 AC 72 ms
71,400 KB
testcase_04 AC 75 ms
71,236 KB
testcase_05 AC 82 ms
75,604 KB
testcase_06 AC 200 ms
82,224 KB
testcase_07 AC 252 ms
92,244 KB
testcase_08 AC 89 ms
76,024 KB
testcase_09 AC 84 ms
75,840 KB
testcase_10 AC 297 ms
90,180 KB
testcase_11 AC 696 ms
162,312 KB
testcase_12 AC 303 ms
124,724 KB
testcase_13 AC 311 ms
108,304 KB
testcase_14 AC 318 ms
109,252 KB
testcase_15 AC 602 ms
144,208 KB
testcase_16 AC 191 ms
86,004 KB
testcase_17 AC 377 ms
110,808 KB
testcase_18 AC 525 ms
132,528 KB
testcase_19 AC 522 ms
132,324 KB
testcase_20 AC 351 ms
108,340 KB
testcase_21 AC 347 ms
107,704 KB
testcase_22 AC 500 ms
121,300 KB
testcase_23 AC 248 ms
91,380 KB
testcase_24 AC 459 ms
121,236 KB
testcase_25 AC 210 ms
88,132 KB
testcase_26 AC 272 ms
97,352 KB
testcase_27 AC 452 ms
121,652 KB
testcase_28 AC 334 ms
107,636 KB
testcase_29 AC 517 ms
124,468 KB
testcase_30 AC 200 ms
87,560 KB
testcase_31 AC 174 ms
81,504 KB
testcase_32 AC 223 ms
90,728 KB
testcase_33 AC 443 ms
122,932 KB
testcase_34 AC 186 ms
82,256 KB
testcase_35 AC 266 ms
95,584 KB
testcase_36 AC 292 ms
101,476 KB
testcase_37 AC 484 ms
122,172 KB
testcase_38 AC 383 ms
109,044 KB
testcase_39 AC 168 ms
81,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import heapq
INF = 10 ** 16
class Dijkstra():
    def __init__(self, N: int) -> None:
        self.N = N 
        self.G = [[] for _ in range(N)]
        return
    
    # 辺の追加
    def addEdge(self, fromNode: int, toNode: int, cost: int):
        self.G[fromNode].append((cost, toNode))
        return
    
    def build(self, startNode: int):
        hq = []
        heapq.heapify(hq)
        # Set start info
        dist = [INF] * self.N
        # prev = [-1] * self.N # 経路復元する場合は移動時に直前の頂点や辺を記録して遷移していく。
        heapq.heappush(hq, (0, startNode))
        dist[startNode] = 0
        # dijkstra
        while hq:
            min_cost, now = heapq.heappop(hq)
            if min_cost > dist[now]:
                continue
            for cost, next in self.G[now]:
                if dist[next] > dist[now] + cost:
                    dist[next] = dist[now] + cost
                    # prev[next] = now # 頂点nextに至る直前の頂点を更新。
                    heapq.heappush(hq, (dist[next], next))
        return dist

def main():
    N, M = map(int, input().split())
    A, B = [], []
    city = set()
    for i in range(M):
        aa, bb = map(int, input().split())
        A.append(aa)
        B.append(bb)
        city.add(aa)
        city.add(bb)
    compressed = {}
    compressed_to_raw = []
    city.add(1)
    city.add(N)
    city = sorted(list(city))
    for index, val in enumerate(city):
        compressed[val] = index
        compressed_to_raw.append(val)
    dk = Dijkstra(len(city))
    for i in range(len(city) - 1):
        aa = city[i]
        bb = city[i + 1]
        s = compressed[aa]
        t = compressed[bb]
        d = (bb - aa) * 2
        dk.addEdge(s, t, d)
    for aa, bb in zip(A, B):
        s = compressed[aa]
        t = compressed[bb]
        d = 2 * bb - 2 * aa - 1
        dk.addEdge(s, t, d)
    res = dk.build(startNode=0)
    print(res[-1])
    return


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