結果

問題 No.848 なかよし旅行
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-26 00:40:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 548 ms / 2,000 ms
コード長 1,177 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 103,692 KB
最終ジャッジ日時 2023-08-07 20:06:43
合計ジャッジ時間 7,814 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 548 ms
103,692 KB
testcase_01 AC 77 ms
71,432 KB
testcase_02 AC 76 ms
71,256 KB
testcase_03 AC 78 ms
71,600 KB
testcase_04 AC 78 ms
71,344 KB
testcase_05 AC 77 ms
71,248 KB
testcase_06 AC 90 ms
76,036 KB
testcase_07 AC 77 ms
71,208 KB
testcase_08 AC 121 ms
78,256 KB
testcase_09 AC 131 ms
78,084 KB
testcase_10 AC 119 ms
78,064 KB
testcase_11 AC 266 ms
82,280 KB
testcase_12 AC 281 ms
83,368 KB
testcase_13 AC 300 ms
83,316 KB
testcase_14 AC 246 ms
81,296 KB
testcase_15 AC 302 ms
83,852 KB
testcase_16 AC 352 ms
88,944 KB
testcase_17 AC 303 ms
84,832 KB
testcase_18 AC 261 ms
81,472 KB
testcase_19 AC 264 ms
81,040 KB
testcase_20 AC 158 ms
79,072 KB
testcase_21 AC 306 ms
85,556 KB
testcase_22 AC 228 ms
87,156 KB
testcase_23 AC 203 ms
78,800 KB
testcase_24 AC 77 ms
71,252 KB
testcase_25 AC 355 ms
87,960 KB
testcase_26 AC 75 ms
71,352 KB
testcase_27 AC 74 ms
71,428 KB
testcase_28 AC 75 ms
71,348 KB
testcase_29 AC 76 ms
71,248 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