結果

問題 No.848 なかよし旅行
ユーザー tobusakanatobusakana
提出日時 2022-11-29 00:47:38
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,521 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 113,596 KB
最終ジャッジ日時 2024-04-15 22:04:31
合計ジャッジ時間 8,515 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 600 ms
113,152 KB
testcase_01 AC 45 ms
53,888 KB
testcase_02 WA -
testcase_03 AC 43 ms
52,992 KB
testcase_04 AC 42 ms
53,248 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 44 ms
53,120 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 420 ms
97,772 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 143 ms
77,988 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 41 ms
52,352 KB
testcase_25 WA -
testcase_26 AC 42 ms
52,608 KB
testcase_27 AC 43 ms
52,480 KB
testcase_28 AC 42 ms
52,608 KB
testcase_29 AC 41 ms
52,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline

N,M,P,Q,T = map(int,readline().split())
P -= 1
Q -= 1
G = [[] for i in range(N)]
for _ in range(M):
    a,b,c = map(int,readline().split())
    G[a - 1].append([b - 1, c])
    G[b - 1].append([a - 1, c])
    
INF = 1 << 60
import heapq as hq
def get_dist_from(x):
    dist = [INF] * N
    q = []
    hq.heappush(q, (0, x))
    while q:
        d, v = hq.heappop(q)
        if dist[v] != INF:
            continue
        dist[v] = d
        for child, c in G[v]:
            if dist[child] != INF:
                continue
            hq.heappush(q, (d + c, child))
    return dist
    
dist_from_0 = get_dist_from(0)
dist_from_P = get_dist_from(P)
dist_from_Q = get_dist_from(Q)


if dist_from_0[P] + dist_from_Q[P] + dist_from_0[Q] <= T:
    print(T)
    exit(0)
    
if max(dist_from_0[P], dist_from_0[Q]) * 2 > T:
    print(-1)
    exit(0)

ans = 0
for v in range(N):
    # vまで一緒に行動して、以後別行動パターン
    time = dist_from_0[v] + max(dist_from_Q[v] + dist_from_Q[0], dist_from_P[v] + dist_from_P[0])
    if time <= T:
        without = max(dist_from_Q[v] + dist_from_Q[0], dist_from_P[v] + dist_from_P[0])
        stay = T - without
        ans = max(ans, stay)

    # vまで一緒に行動、P,Qを訪れて、またvに合流するパターン
    time = dist_from_0[v] * 2 + max(dist_from_P[v] * 2, dist_from_Q[v] * 2)
    if time <= T:
        stay = T - max(dist_from_P[v] * 2, dist_from_Q[v] * 2)
        ans = max(ans, stay)
print(ans)
0