結果

問題 No.614 壊れたキャンパス
ユーザー mkawa2
提出日時 2020-06-04 22:13:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,441 ms / 2,000 ms
コード長 1,372 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 262,292 KB
最終ジャッジ日時 2024-11-30 08:19:09
合計ジャッジ時間 16,640 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *
import sys

def MI(): return map(int, sys.stdin.readline().split())

def main():
    n, m, k, s, t = MI()

    abc = []
    floor = [set() for _ in range(n)]
    floor[0].add(s)
    floor[n - 1].add(t)
    for _ in range(m):
        a, b, c = MI()
        floor[a - 1].add(b)
        floor[a].add(c)
        abc.append((a-1, b, c))

    iftou = {}
    u = 0
    for i in range(n):
        for f in floor[i]:
            iftou[i, f] = u
            u += 1
    un = u

    s = iftou[0, s]
    t = iftou[n - 1, t]

    to = [[] for _ in range(un)]
    for a, b, c in abc:
        u = iftou[a, b]
        v = iftou[a + 1, c]
        to[u].append((v, 0))
    for i in range(n):
        v = -1
        pf = -1
        for f in sorted(floor[i]):
            if v == -1:
                v = iftou[i, f]
                pf = f
                continue
            u = iftou[i, f]
            cost = f - pf
            to[u].append((v, cost))
            to[v].append((u, cost))
            v = u
            pf = f

    hp = []
    heappush(hp, (0, s))
    dist = [-1] * un
    while hp:
        d, u = heappop(hp)
        if u == t:
            print(d)
            exit()
        if dist[u] != -1: continue
        dist[u] = d
        for v, cost in to[u]:
            if dist[v] != -1: continue
            heappush(hp, (d + cost, v))

    print(-1)

main()
0