結果

問題 No.2805 Go to School
ユーザー 2000 Ekiben2000 Ekiben
提出日時 2024-07-12 22:04:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,623 ms / 2,000 ms
コード長 1,431 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 81,844 KB
実行使用メモリ 266,572 KB
最終ジャッジ日時 2024-07-16 01:40:14
合計ジャッジ時間 24,890 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
54,016 KB
testcase_01 AC 45 ms
54,272 KB
testcase_02 AC 45 ms
54,144 KB
testcase_03 AC 47 ms
53,760 KB
testcase_04 AC 565 ms
213,028 KB
testcase_05 AC 812 ms
171,112 KB
testcase_06 AC 571 ms
124,940 KB
testcase_07 AC 479 ms
121,012 KB
testcase_08 AC 662 ms
166,104 KB
testcase_09 AC 524 ms
124,320 KB
testcase_10 AC 458 ms
125,064 KB
testcase_11 AC 1,623 ms
266,112 KB
testcase_12 AC 1,030 ms
178,736 KB
testcase_13 AC 1,407 ms
217,560 KB
testcase_14 AC 340 ms
108,604 KB
testcase_15 AC 45 ms
53,760 KB
testcase_16 AC 47 ms
54,144 KB
testcase_17 AC 46 ms
53,760 KB
testcase_18 AC 695 ms
139,012 KB
testcase_19 AC 594 ms
127,056 KB
testcase_20 AC 1,047 ms
182,316 KB
testcase_21 AC 1,446 ms
264,112 KB
testcase_22 AC 697 ms
163,144 KB
testcase_23 AC 1,065 ms
178,044 KB
testcase_24 AC 1,046 ms
176,004 KB
testcase_25 AC 373 ms
134,012 KB
testcase_26 AC 1,055 ms
266,572 KB
testcase_27 AC 607 ms
205,604 KB
testcase_28 AC 104 ms
82,432 KB
testcase_29 AC 146 ms
101,248 KB
testcase_30 AC 318 ms
153,916 KB
testcase_31 AC 550 ms
146,012 KB
testcase_32 AC 837 ms
264,060 KB
testcase_33 AC 1,204 ms
256,872 KB
testcase_34 AC 75 ms
67,840 KB
testcase_35 AC 75 ms
69,248 KB
testcase_36 AC 662 ms
139,848 KB
testcase_37 AC 609 ms
162,856 KB
testcase_38 AC 1,096 ms
216,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq, sys
from collections import defaultdict

input = sys.stdin.read
data = input().split()

N, M, L, S, E = map(int, data[:5])
paths = [(int(data[i]), int(data[i+1]), int(data[i+2])) for i in range(5, 5 + 3 * M, 3)]
toilets = list(map(int, data[5 + 3 * M:]))

g = defaultdict(dict)
for a, b, t in paths:
    g[a][b] = min(g[a].get(b, float('inf')), t)
    g[b][a] = min(g[b].get(a, float('inf')), t)

def dijkstra(g, start):
    d = {i: float('inf') for i in g}
    d[start] = 0
    q = [(0, start)]
    while q:
        cd, cn = heapq.heappop(q)
        if cd > d[cn]: continue
        for n, w in g[cn].items():
            dist = cd + w
            if dist < d[n]:
                d[n] = dist
                heapq.heappush(q, (dist, n))
    return d

min_time_to = dijkstra(g, 1)
reverse_g = defaultdict(dict)
for a in g:
    for b in g[a]:
        reverse_g[b][a] = g[a][b]
min_time_from = dijkstra(reverse_g, N)

min_time = float('inf')
for toilet in toilets:
    if toilet in min_time_to and toilet in min_time_from:
        arrival_time = min_time_to[toilet]
        if arrival_time < S:
            total_time = S + 1 + min_time_from[toilet]
        elif arrival_time <= S + E - 0.99:
            total_time = arrival_time + 1 + min_time_from[toilet]
        else:
            continue
        if total_time < min_time:
            min_time = total_time

print(-1 if min_time == float('inf') else int(min_time))
0