結果

問題 No.848 なかよし旅行
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-26 00:40:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 502 ms / 2,000 ms
コード長 1,177 bytes
コンパイル時間 146 ms
コンパイル使用メモリ 82,480 KB
実行使用メモリ 102,048 KB
最終ジャッジ日時 2024-04-25 14:22:50
合計ジャッジ時間 6,247 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 502 ms
102,048 KB
testcase_01 AC 40 ms
53,788 KB
testcase_02 AC 38 ms
53,880 KB
testcase_03 AC 39 ms
53,076 KB
testcase_04 AC 39 ms
53,252 KB
testcase_05 AC 38 ms
53,560 KB
testcase_06 AC 51 ms
63,540 KB
testcase_07 AC 40 ms
54,312 KB
testcase_08 AC 89 ms
76,948 KB
testcase_09 AC 99 ms
77,152 KB
testcase_10 AC 85 ms
76,676 KB
testcase_11 AC 229 ms
80,852 KB
testcase_12 AC 241 ms
81,408 KB
testcase_13 AC 256 ms
82,784 KB
testcase_14 AC 207 ms
79,256 KB
testcase_15 AC 266 ms
83,188 KB
testcase_16 AC 307 ms
87,404 KB
testcase_17 AC 263 ms
83,248 KB
testcase_18 AC 223 ms
80,188 KB
testcase_19 AC 232 ms
79,680 KB
testcase_20 AC 122 ms
77,720 KB
testcase_21 AC 260 ms
83,820 KB
testcase_22 AC 193 ms
86,208 KB
testcase_23 AC 170 ms
77,504 KB
testcase_24 AC 38 ms
53,656 KB
testcase_25 AC 318 ms
86,340 KB
testcase_26 AC 39 ms
53,808 KB
testcase_27 AC 38 ms
53,500 KB
testcase_28 AC 39 ms
54,204 KB
testcase_29 AC 37 ms
53,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
inf = 10**18


N, M, P, Q, T = map(int, input().split())
P -= 1
Q -= 1
G = [[] for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append((b, c))
    G[b].append((a, c))


def dijkstra(start):
    dist = [inf] * N
    dist[start] = 0
    que = [(0, start)]
    while que:
        ds, s = heapq.heappop(que)
        if dist[s] < ds:
            continue
        for t, dt in G[s]:
            if dist[t] > ds + dt:
                dist[t] = ds + dt
                heapq.heappush(que, (ds + dt, t))
    return dist


dist0 = dijkstra(0)
distP = dijkstra(P)
distQ = dijkstra(Q)

if dist0[P] + dist0[Q] + distP[Q] <= T:
    print(T)
    exit()

if 2 * max(dist0[P], dist0[Q]) > T:
    print(-1)
    exit()

ans = 0
for i in range(N):
    go = dist0[i]
    to_p = distP[i]
    to_q = distQ[i]
    for j in range(N):
        from_p = distP[j]
        from_q = distQ[j]
        back = dist0[j]
        separate = max(to_p + from_p, to_q + from_q)
        if go + back + separate <= T:
            ans = max(ans, T - separate)

print(ans)
0