結果

問題 No.1995 CHIKA Road
ユーザー kept1994kept1994
提出日時 2022-10-30 19:28:30
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,027 bytes
コンパイル時間 349 ms
コンパイル使用メモリ 87,172 KB
実行使用メモリ 162,116 KB
最終ジャッジ日時 2023-09-21 17:51:53
合計ジャッジ時間 15,886 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,464 KB
testcase_01 AC 72 ms
71,312 KB
testcase_02 AC 72 ms
71,512 KB
testcase_03 AC 71 ms
71,496 KB
testcase_04 AC 73 ms
71,464 KB
testcase_05 WA -
testcase_06 AC 171 ms
82,268 KB
testcase_07 AC 247 ms
92,508 KB
testcase_08 AC 88 ms
76,144 KB
testcase_09 AC 82 ms
75,676 KB
testcase_10 AC 292 ms
90,288 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 193 ms
86,196 KB
testcase_17 AC 380 ms
110,656 KB
testcase_18 AC 533 ms
132,484 KB
testcase_19 AC 528 ms
132,672 KB
testcase_20 AC 345 ms
108,364 KB
testcase_21 AC 345 ms
107,936 KB
testcase_22 AC 499 ms
121,412 KB
testcase_23 AC 247 ms
91,460 KB
testcase_24 AC 467 ms
121,432 KB
testcase_25 AC 218 ms
88,036 KB
testcase_26 AC 269 ms
97,428 KB
testcase_27 AC 456 ms
122,172 KB
testcase_28 AC 336 ms
107,468 KB
testcase_29 AC 520 ms
124,436 KB
testcase_30 AC 200 ms
88,016 KB
testcase_31 AC 173 ms
81,500 KB
testcase_32 AC 218 ms
90,980 KB
testcase_33 AC 442 ms
122,868 KB
testcase_34 AC 186 ms
82,652 KB
testcase_35 AC 267 ms
95,692 KB
testcase_36 AC 292 ms
101,436 KB
testcase_37 AC 482 ms
122,412 KB
testcase_38 AC 381 ms
109,328 KB
testcase_39 AC 168 ms
81,472 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(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