結果

問題 No.848 なかよし旅行
ユーザー kurimupykurimupy
提出日時 2020-06-19 00:24:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,158 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 10,900 KB
実行使用メモリ 46,540 KB
最終ジャッジ日時 2023-09-16 12:32:10
合計ジャッジ時間 20,376 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,105 ms
46,540 KB
testcase_01 AC 17 ms
8,312 KB
testcase_02 AC 17 ms
8,068 KB
testcase_03 AC 17 ms
8,080 KB
testcase_04 AC 17 ms
8,232 KB
testcase_05 AC 17 ms
8,068 KB
testcase_06 AC 19 ms
8,260 KB
testcase_07 AC 18 ms
8,264 KB
testcase_08 AC 44 ms
8,176 KB
testcase_09 AC 46 ms
8,532 KB
testcase_10 AC 44 ms
8,088 KB
testcase_11 AC 941 ms
15,908 KB
testcase_12 AC 1,182 ms
18,400 KB
testcase_13 AC 1,483 ms
22,604 KB
testcase_14 AC 1,045 ms
11,388 KB
testcase_15 AC 1,278 ms
21,880 KB
testcase_16 AC 1,897 ms
32,504 KB
testcase_17 AC 1,129 ms
23,904 KB
testcase_18 AC 950 ms
15,140 KB
testcase_19 AC 979 ms
14,152 KB
testcase_20 AC 34 ms
9,252 KB
testcase_21 AC 1,297 ms
27,624 KB
testcase_22 AC 591 ms
29,352 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq


def dijkstra(s, graph):
    n = len(graph)-1
    dist = [float("inf") for i in range(n+1)]
    dist[s] = 0
    pq = []
    heapq.heapify(pq)
    heapq.heappush(pq, (0, s))
    while pq:
        mini_dis, node = heapq.heappop(pq)
        if dist[node] < mini_dis:
            continue
        for w, point in graph[node]:
            if dist[point] < w:
                continue
            newlen = dist[node]+w
            if newlen < dist[point]:
                heapq.heappush(pq, (newlen, point))
                dist[point] = newlen
    return dist


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


A = dijkstra(1, graph)
B = dijkstra(P, graph)
C = dijkstra(Q, graph)


if max(2*A[P], 2*A[Q]) > T:
    print(-1)
    exit()

if (A[P]+A[Q]+C[P]) <= T:
    print(T)
    exit()
else:
    ans = 0
    for x in range(1, N+1):
        for y in range(1, N+1):
            if A[x]+B[x]+B[y]+A[y] <= T and A[x]+C[x]+C[y]+A[y] <= T:
                ans = max(ans,T-max(B[x]+B[y],C[x]+C[y]))
    print(ans)
0