結果

問題 No.848 なかよし旅行
ユーザー nephrologistnephrologist
提出日時 2020-03-24 12:57:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 694 ms / 2,000 ms
コード長 2,101 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 103,800 KB
最終ジャッジ日時 2023-08-07 20:00:22
合計ジャッジ時間 10,315 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 573 ms
103,800 KB
testcase_01 AC 82 ms
71,280 KB
testcase_02 AC 79 ms
71,576 KB
testcase_03 AC 80 ms
71,520 KB
testcase_04 AC 80 ms
71,340 KB
testcase_05 AC 79 ms
71,340 KB
testcase_06 AC 111 ms
77,204 KB
testcase_07 AC 81 ms
71,292 KB
testcase_08 AC 129 ms
77,768 KB
testcase_09 AC 173 ms
78,932 KB
testcase_10 AC 142 ms
78,600 KB
testcase_11 AC 357 ms
85,040 KB
testcase_12 AC 429 ms
88,956 KB
testcase_13 AC 501 ms
92,168 KB
testcase_14 AC 292 ms
80,916 KB
testcase_15 AC 486 ms
91,576 KB
testcase_16 AC 694 ms
103,228 KB
testcase_17 AC 483 ms
93,148 KB
testcase_18 AC 344 ms
84,552 KB
testcase_19 AC 334 ms
82,504 KB
testcase_20 AC 174 ms
79,492 KB
testcase_21 AC 550 ms
96,132 KB
testcase_22 AC 510 ms
102,120 KB
testcase_23 AC 239 ms
78,444 KB
testcase_24 AC 77 ms
71,124 KB
testcase_25 AC 648 ms
99,120 KB
testcase_26 AC 76 ms
71,260 KB
testcase_27 AC 76 ms
71,136 KB
testcase_28 AC 76 ms
71,424 KB
testcase_29 AC 75 ms
71,396 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