結果

問題 No.848 なかよし旅行
ユーザー ttrttr
提出日時 2020-02-17 22:12:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 606 ms / 2,000 ms
コード長 1,042 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 82,596 KB
実行使用メモリ 103,280 KB
最終ジャッジ日時 2024-04-25 14:11:18
合計ジャッジ時間 8,541 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 526 ms
103,280 KB
testcase_01 AC 37 ms
54,204 KB
testcase_02 AC 35 ms
53,108 KB
testcase_03 AC 37 ms
55,168 KB
testcase_04 AC 37 ms
55,048 KB
testcase_05 AC 38 ms
54,868 KB
testcase_06 AC 73 ms
75,200 KB
testcase_07 AC 38 ms
54,988 KB
testcase_08 AC 92 ms
76,396 KB
testcase_09 AC 145 ms
78,084 KB
testcase_10 AC 110 ms
77,284 KB
testcase_11 AC 291 ms
83,156 KB
testcase_12 AC 354 ms
86,924 KB
testcase_13 AC 433 ms
91,060 KB
testcase_14 AC 239 ms
79,436 KB
testcase_15 AC 403 ms
90,080 KB
testcase_16 AC 606 ms
103,016 KB
testcase_17 AC 401 ms
91,960 KB
testcase_18 AC 290 ms
82,968 KB
testcase_19 AC 276 ms
82,112 KB
testcase_20 AC 136 ms
77,620 KB
testcase_21 AC 488 ms
96,736 KB
testcase_22 AC 459 ms
102,988 KB
testcase_23 AC 175 ms
77,956 KB
testcase_24 AC 36 ms
53,292 KB
testcase_25 AC 581 ms
99,512 KB
testcase_26 AC 36 ms
52,744 KB
testcase_27 AC 35 ms
53,352 KB
testcase_28 AC 35 ms
53,148 KB
testcase_29 AC 36 ms
53,412 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