結果

問題 No.848 なかよし旅行
ユーザー kurimupykurimupy
提出日時 2020-06-19 00:23:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 547 ms / 2,000 ms
コード長 1,158 bytes
コンパイル時間 213 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 104,136 KB
最終ジャッジ日時 2024-04-25 14:17:34
合計ジャッジ時間 6,773 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 547 ms
104,136 KB
testcase_01 AC 39 ms
54,312 KB
testcase_02 AC 35 ms
53,352 KB
testcase_03 AC 36 ms
53,940 KB
testcase_04 AC 36 ms
54,296 KB
testcase_05 AC 36 ms
54,568 KB
testcase_06 AC 52 ms
65,700 KB
testcase_07 AC 37 ms
53,928 KB
testcase_08 AC 87 ms
76,828 KB
testcase_09 AC 110 ms
77,540 KB
testcase_10 AC 92 ms
77,168 KB
testcase_11 AC 249 ms
81,212 KB
testcase_12 AC 266 ms
83,040 KB
testcase_13 AC 281 ms
84,476 KB
testcase_14 AC 243 ms
80,972 KB
testcase_15 AC 277 ms
83,560 KB
testcase_16 AC 348 ms
89,400 KB
testcase_17 AC 281 ms
84,496 KB
testcase_18 AC 262 ms
81,876 KB
testcase_19 AC 254 ms
80,904 KB
testcase_20 AC 139 ms
78,468 KB
testcase_21 AC 302 ms
87,000 KB
testcase_22 AC 233 ms
87,364 KB
testcase_23 AC 216 ms
78,076 KB
testcase_24 AC 38 ms
52,684 KB
testcase_25 AC 412 ms
89,392 KB
testcase_26 AC 37 ms
52,984 KB
testcase_27 AC 37 ms
53,204 KB
testcase_28 AC 36 ms
52,944 KB
testcase_29 AC 36 ms
53,500 KB
権限があれば一括ダウンロードができます

ソースコード

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