結果

問題 No.614 壊れたキャンパス
ユーザー H3PO4H3PO4
提出日時 2022-04-25 09:44:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,879 ms / 2,000 ms
コード長 1,673 bytes
コンパイル時間 317 ms
コンパイル使用メモリ 86,852 KB
実行使用メモリ 271,080 KB
最終ジャッジ日時 2023-09-09 12:09:58
合計ジャッジ時間 21,524 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,412 KB
testcase_01 AC 79 ms
71,192 KB
testcase_02 AC 78 ms
71,156 KB
testcase_03 AC 77 ms
70,964 KB
testcase_04 AC 77 ms
71,180 KB
testcase_05 AC 77 ms
71,364 KB
testcase_06 AC 80 ms
71,500 KB
testcase_07 AC 81 ms
71,300 KB
testcase_08 AC 1,735 ms
269,656 KB
testcase_09 AC 1,729 ms
252,508 KB
testcase_10 AC 1,305 ms
239,036 KB
testcase_11 AC 1,879 ms
270,040 KB
testcase_12 AC 1,823 ms
253,384 KB
testcase_13 AC 1,878 ms
270,856 KB
testcase_14 AC 1,760 ms
271,080 KB
testcase_15 AC 1,288 ms
270,044 KB
testcase_16 AC 1,693 ms
269,988 KB
testcase_17 AC 993 ms
240,156 KB
testcase_18 AC 1,083 ms
252,864 KB
testcase_19 AC 849 ms
250,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from heapq import heappop, heappush

input = sys.stdin.buffer.readline

INF = 10 ** 18


def main():
    def dijkstra(N, G, source, target):
        dist = [INF] * N
        que = [(0, source)]
        dist[source] = 0
        while que:
            c, v = heappop(que)
            if dist[v] < c:
                continue
            for t, cost in G[v]:
                if dist[v] + cost < dist[t]:
                    dist[t] = dist[v] + cost
                    heappush(que, (dist[t], t))
        return dist[target]

    N, M, K, S, T = map(int, input().split())
    S -= 1
    T -= 1
    passages = tuple(tuple(int(x) - 1 for x in input().split()) for _ in range(M))

    node_S = (0, S)
    node_T = (N - 1, T)
    node_set = {node_S, node_T}
    for a, b, c in passages:
        node_set.add((a, b))
        node_set.add((a + 1, c))
    node_dict = {t: i for i, t in enumerate(node_set)}

    L = len(node_set)
    G = [[] for _ in range(L)]

    def add_edge(s_bldg, s_floor, t_bldg, t_floor, cost):
        s = node_dict[(s_bldg, s_floor)]
        t = node_dict[(t_bldg, t_floor)]
        G[s].append((t, cost))

    for a, b, c in passages:
        add_edge(a, b, a + 1, c, 0)

    towers = [[] for _ in range(N)]
    for a, b in node_set:
        towers[a].append(b)
    for i in range(N):
        towers[i].sort()
        for j in range(len(towers[i]) - 1):
            f1 = towers[i][j]
            f2 = towers[i][j + 1]
            assert f2 > f1
            add_edge(i, f1, i, f2, f2 - f1)
            add_edge(i, f2, i, f1, f2 - f1)

    ans = dijkstra(L, G, node_dict[node_S], node_dict[node_T])

    print(ans if ans != INF else -1)


main()
0