結果

問題 No.848 なかよし旅行
ユーザー ttrttr
提出日時 2020-02-17 22:12:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 719 ms / 2,000 ms
コード長 1,042 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 87,056 KB
実行使用メモリ 105,016 KB
最終ジャッジ日時 2023-08-07 19:59:26
合計ジャッジ時間 10,690 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 633 ms
105,016 KB
testcase_01 AC 79 ms
71,404 KB
testcase_02 AC 77 ms
71,404 KB
testcase_03 AC 77 ms
71,464 KB
testcase_04 AC 79 ms
71,204 KB
testcase_05 AC 78 ms
71,420 KB
testcase_06 AC 119 ms
77,588 KB
testcase_07 AC 79 ms
71,472 KB
testcase_08 AC 134 ms
77,996 KB
testcase_09 AC 193 ms
79,412 KB
testcase_10 AC 150 ms
78,532 KB
testcase_11 AC 365 ms
85,536 KB
testcase_12 AC 436 ms
89,060 KB
testcase_13 AC 528 ms
92,384 KB
testcase_14 AC 298 ms
80,804 KB
testcase_15 AC 503 ms
92,640 KB
testcase_16 AC 719 ms
104,708 KB
testcase_17 AC 506 ms
95,540 KB
testcase_18 AC 352 ms
84,472 KB
testcase_19 AC 346 ms
82,828 KB
testcase_20 AC 190 ms
79,692 KB
testcase_21 AC 590 ms
98,024 KB
testcase_22 AC 563 ms
104,380 KB
testcase_23 AC 229 ms
79,648 KB
testcase_24 AC 77 ms
71,488 KB
testcase_25 AC 699 ms
101,360 KB
testcase_26 AC 76 ms
71,268 KB
testcase_27 AC 77 ms
71,360 KB
testcase_28 AC 77 ms
71,248 KB
testcase_29 AC 77 ms
71,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import heapq
def dijkstra(s):
    inf = 10**18
    dist = [inf]*N
    dist[s] = 0
    h = []
    for e in E[s]:
        heapq.heappush(h, e)
    while h:
        temp = heapq.heappop(h)
        d = temp[0]
        v = temp[1]
        if dist[v] < inf:
            continue
        dist[v] = d
        for e in E[v]:
            if dist[e[1]] < inf:
                continue
            heapq.heappush(h, (e[0]+d, e[1]))
    return dist

dist_0 = dijkstra(0)
dist_p = dijkstra(P-1)
dist_q = dijkstra(Q-1)
if dist_0[P-1]+dist_p[Q-1]+dist_q[0] <= T:
    ans = T
elif dist_0[P-1]*2 > T or dist_0[Q-1]*2 > T:
    ans = -1
else:
    ans = 0
    for i in range(N):
        for j in range(N):
            if dist_0[i]+max(dist_p[i]+dist_p[j], dist_q[i]+dist_q[j])+dist_0[j] <= T:
                ans = max(ans, T-max(dist_p[i]+dist_p[j], dist_q[i]+dist_q[j]))

print(ans)
0