結果

問題 No.614 壊れたキャンパス
ユーザー LyricalMaestroLyricalMaestro
提出日時 2024-02-08 22:12:17
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,222 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 435,192 KB
最終ジャッジ日時 2024-02-08 22:12:47
合計ジャッジ時間 28,574 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 37 ms
53,460 KB
testcase_03 AC 37 ms
53,460 KB
testcase_04 AC 37 ms
53,460 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 38 ms
53,460 KB
testcase_07 AC 36 ms
53,460 KB
testcase_08 TLE -
testcase_09 TLE -
testcase_10 AC 994 ms
278,736 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 867 ms
258,896 KB
testcase_16 TLE -
testcase_17 AC 1,510 ms
435,192 KB
testcase_18 TLE -
testcase_19 AC 1,508 ms
380,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/614

import heapq

def main():
    N, M, K, S, T = map(int, input().split())
    abc = []
    for _ in range(M):
        a, b, c = map(int, input().split())
        abc.append((a - 1, b, c))
    
    map_array = [[] for _ in range(N)]
    for a, b, c in abc:
        map_array[a].append(b)
        map_array[a + 1].append(c)
    map_array[0].append(S)
    map_array[-1].append(T)
    
    coord_map_array = [{} for _ in range(N)]
    for i, values in enumerate(map_array):
        values.sort()
        for j, value in enumerate(values):
            coord_map_array[i][value] = j
    
    # グラフ構築
    next_nodes = [{} for _ in range(N)]
    for a, b, c in abc:
        b_i = coord_map_array[a][b]
        c_i = coord_map_array[a + 1][c]
        if b_i not in next_nodes[a]:
            next_nodes[a][b_i] = []
        next_nodes[a][b_i].append((a + 1, c_i, 0))
    
    for i, values in enumerate(map_array):
        if len(values) == 0:
            continue
        for j in range(len(values) - 1):
            a = values[j]
            b = values[j + 1]
            if j not in next_nodes[i]:
                next_nodes[i][j] = []
            if (j + 1) not in next_nodes[i]:
                next_nodes[i][j + 1] = []

            next_nodes[i][j].append((i, j + 1, b - a))
            next_nodes[i][j + 1].append((i, j, b - a))
    
    # ダイクストラ法で解く
    s_ = coord_map_array[0][S]
    t_ = coord_map_array[-1][T]
    fix = {}
    seen = {(0, s_):0}
    queue = []
    heapq.heappush(queue, (0, 0, s_)) 
    while len(queue) > 0:
        cost, a, k = heapq.heappop(queue)
        if (a, k) in fix:
            continue

        fix[(a, k)] = cost
        if k not in next_nodes[a]:
            continue

        for b, c, d in next_nodes[a][k]:
            if (b, c) in fix:
                continue

            new_cost = cost + d
            if (b, c) not in seen or seen[(b, c)] > new_cost:
                seen[(b, c)] = new_cost
                heapq.heappush(queue, (new_cost, b, c))
    
    if (N - 1, t_) not in fix:
        print(-1)
    else:
        print(fix[(N -1 , t_)])
                

    







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