結果

問題 No.848 なかよし旅行
ユーザー irumo8202irumo8202
提出日時 2022-02-06 22:00:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 627 ms / 2,000 ms
コード長 1,099 bytes
コンパイル時間 1,751 ms
コンパイル使用メモリ 86,884 KB
実行使用メモリ 105,512 KB
最終ジャッジ日時 2023-09-02 07:17:13
合計ジャッジ時間 10,381 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 627 ms
105,512 KB
testcase_01 AC 81 ms
71,292 KB
testcase_02 AC 76 ms
71,532 KB
testcase_03 AC 76 ms
71,140 KB
testcase_04 AC 75 ms
71,276 KB
testcase_05 AC 76 ms
71,276 KB
testcase_06 AC 90 ms
76,068 KB
testcase_07 AC 78 ms
71,472 KB
testcase_08 AC 127 ms
78,180 KB
testcase_09 AC 149 ms
79,152 KB
testcase_10 AC 130 ms
77,872 KB
testcase_11 AC 300 ms
82,344 KB
testcase_12 AC 320 ms
83,572 KB
testcase_13 AC 341 ms
84,328 KB
testcase_14 AC 268 ms
80,780 KB
testcase_15 AC 347 ms
84,136 KB
testcase_16 AC 421 ms
89,700 KB
testcase_17 AC 357 ms
85,920 KB
testcase_18 AC 300 ms
81,656 KB
testcase_19 AC 301 ms
81,564 KB
testcase_20 AC 233 ms
79,736 KB
testcase_21 AC 369 ms
87,656 KB
testcase_22 AC 281 ms
89,240 KB
testcase_23 AC 226 ms
78,736 KB
testcase_24 AC 74 ms
71,436 KB
testcase_25 AC 426 ms
89,040 KB
testcase_26 AC 74 ms
71,284 KB
testcase_27 AC 75 ms
71,260 KB
testcase_28 AC 76 ms
71,320 KB
testcase_29 AC 75 ms
71,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush

INF = 1 << 60


def dijkstra(s, edge):
    n = len(edge)
    dist = [INF] * n
    dist[s] = 0
    hq = []
    heappush(hq, (dist[s], s))

    while hq:
        dist_v, v = heappop(hq)
        if dist_v > dist[v]:
            continue
        for cost, nx in edge[v]:
            if dist[nx] > dist[v] + cost:
                dist[nx] = dist[v] + cost
                heappush(hq, (dist[nx], nx))
    return dist


N, M, P, Q, T = map(int, input().split())
P -= 1
Q -= 1

G = [[] for _ in range(N)]

for _ in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append((c, b))
    G[b].append((c, a))

dist_0 = dijkstra(0, G)
dist_p = dijkstra(P, G)
dist_q = dijkstra(Q, G)


tmp = dist_0[P] + dist_p[Q] + dist_q[0]
if tmp <= T:
    print(T)
    exit()

ans = -1

for u in range(N):
    for v in range(N):
        tmp = dist_0[u]
        tmp += max(dist_p[u] + dist_p[v], dist_q[u] + dist_q[v])
        tmp += dist_0[v]
        if tmp <= T:
            ans = max(ans, T - max(dist_p[u] + dist_p[v], dist_q[u] + dist_q[v]))

print(ans)
0