結果

問題 No.614 壊れたキャンパス
ユーザー brthyyjpbrthyyjp
提出日時 2021-12-30 03:19:12
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,345 bytes
コンパイル時間 225 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 251,896 KB
最終ジャッジ日時 2024-04-15 10:18:15
合計ジャッジ時間 12,806 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
54,832 KB
testcase_01 AC 52 ms
55,516 KB
testcase_02 AC 55 ms
54,860 KB
testcase_03 AC 60 ms
54,780 KB
testcase_04 AC 52 ms
55,776 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1,820 ms
251,896 KB
testcase_10 AC 955 ms
209,416 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 864 ms
199,184 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 788 ms
213,164 KB
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

import heapq
INF = 10**18
def dijkstra(s, edge):
    dist = defaultdict(lambda: INF)
    dist[s] = 0
    edgelist = []
    heapq.heappush(edgelist,(dist[s], s))
    while edgelist:
        minedge = heapq.heappop(edgelist)
        if dist[minedge[1]] < minedge[0]:
            continue
        v = minedge[1]
        for e in edge[v]:
            if dist[e[1]] > dist[v]+e[0]:
                dist[e[1]] = dist[v]+e[0]
                heapq.heappush(edgelist,(dist[e[1]], e[1]))
    return dist

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

def main():

    n,m,k,s,t = map(int, input().split())
    s,t = s-1, t-1
    edge = defaultdict(lambda: [])
    V = [[] for i in range(n)]
    V[0].append(0)
    V[n-1].append(t)
    for i in range(m):
        a, b, c = map(int, input().split())
        a, b, c = a-1, b-1, c-1
        edge[b*n+a].append((0, c*n+a+1))
        V[a].append(b)
        V[a+1].append(c)

    V = [sorted(l) for l in V]

    for a, l in enumerate(V):
        for x, y in zip(l, l[1:]):
            d = abs(x-y)
            edge[x*n+a].append((d, y*n+a))
            edge[y*n+a].append((d, x*n+a))

    t = t*n+n-1
    D = dijkstra(s, edge)
    if D[t] < INF:
        print(D[t])
    else:
        print(-1)

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