結果

問題 No.614 壊れたキャンパス
ユーザー LyricalMaestro
提出日時 2024-02-08 22:12:17
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,222 bytes
コンパイル時間 1,009 ms
コンパイル使用メモリ 82,636 KB
実行使用メモリ 435,744 KB
最終ジャッジ日時 2024-09-28 12:53:48
合計ジャッジ時間 27,460 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12 TLE * 8
権限があれば一括ダウンロードができます

ソースコード

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