結果

問題 No.848 なかよし旅行
ユーザー nephrologistnephrologist
提出日時 2020-03-24 12:57:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 651 ms / 2,000 ms
コード長 2,101 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 82,436 KB
実行使用メモリ 102,592 KB
最終ジャッジ日時 2024-04-25 14:14:14
合計ジャッジ時間 8,689 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 520 ms
101,820 KB
testcase_01 AC 40 ms
54,256 KB
testcase_02 AC 38 ms
53,496 KB
testcase_03 AC 40 ms
53,336 KB
testcase_04 AC 39 ms
53,212 KB
testcase_05 AC 40 ms
54,268 KB
testcase_06 AC 70 ms
72,684 KB
testcase_07 AC 39 ms
53,760 KB
testcase_08 AC 92 ms
76,648 KB
testcase_09 AC 136 ms
77,512 KB
testcase_10 AC 107 ms
76,608 KB
testcase_11 AC 311 ms
83,596 KB
testcase_12 AC 382 ms
86,604 KB
testcase_13 AC 463 ms
90,708 KB
testcase_14 AC 261 ms
79,480 KB
testcase_15 AC 451 ms
89,832 KB
testcase_16 AC 651 ms
102,592 KB
testcase_17 AC 440 ms
92,600 KB
testcase_18 AC 302 ms
82,600 KB
testcase_19 AC 295 ms
81,476 KB
testcase_20 AC 135 ms
77,928 KB
testcase_21 AC 504 ms
95,992 KB
testcase_22 AC 469 ms
100,556 KB
testcase_23 AC 201 ms
76,980 KB
testcase_24 AC 37 ms
53,320 KB
testcase_25 AC 612 ms
98,076 KB
testcase_26 AC 39 ms
54,388 KB
testcase_27 AC 38 ms
53,372 KB
testcase_28 AC 39 ms
54,088 KB
testcase_29 AC 38 ms
53,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 場合分けが甘い。
# 全部一緒と不可能のケースはOK
# 最初、途中まで一緒→最後一緒のパターンが甘い。
# 3頂点からの最短距離の組み合わせで行ける。
import sys
input = sys.stdin.buffer.readline

from heapq import heappush, heappop

n, m, p, q, t = map(int, input().split())
graph = [[] for _ in range(n + 1)]
for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a].append((c, b))
    graph[b].append((c, a))
infi = 10 ** 20
dist = [infi] * (n + 1)
used = [False] * (n + 1)
edgelist = []


def dijkstra(start, dist):
    dist[start] = 0
    used[start] = True
    for cost, v in graph[start]:
        heappush(edgelist, (cost, v))
    while edgelist:
        cost, v = heappop(edgelist)
        if used[v]:
            continue
        used[v] = True
        dist[v] = min(cost, dist[v])
        for cost2, w in graph[v]:
            if used[w]:
                continue
            new_cost = cost2 + cost
            heappush(edgelist, (new_cost, w))
    return dist


calculated_dist1 = dijkstra(1, dist)
dist_ap = dist[p]
dist_aq = dist[q]

distp = [infi] * (n + 1)
used = [False] * (n + 1)
edgelist = []
calculated_distp = dijkstra(p, distp)
dist_pq = calculated_distp[q]

distq = [infi] * (n + 1)
used = [False] * (n + 1)
edgelist = []
calculated_distq = dijkstra(q, distq)

longer = max(dist_ap, dist_aq)
if dist_ap + dist_aq + dist_pq <= t:
    print(t)
elif 2 * longer > t:
    print(-1)
else:
    ans = 0
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            total_p = (
                calculated_dist1[i]
                + calculated_distp[i]
                + calculated_distp[j]
                + calculated_dist1[j]
            )
            total_q = (
                calculated_dist1[i]
                + calculated_distq[i]
                + calculated_distq[j]
                + calculated_dist1[j]
            )
            bigger = max(total_p, total_q)
            if bigger <= t:
                ans = max(ans, calculated_dist1[i] + calculated_dist1[j] + t - bigger)
    print(ans)
0