結果

問題 No.848 なかよし旅行
ユーザー tcltktcltk
提出日時 2021-01-31 05:05:21
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 506 ms / 2,000 ms
コード長 1,748 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 103,692 KB
最終ジャッジ日時 2023-08-07 20:07:04
合計ジャッジ時間 7,300 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 506 ms
103,692 KB
testcase_01 AC 78 ms
71,348 KB
testcase_02 AC 77 ms
71,252 KB
testcase_03 AC 77 ms
71,556 KB
testcase_04 AC 76 ms
71,216 KB
testcase_05 AC 77 ms
71,100 KB
testcase_06 AC 87 ms
75,704 KB
testcase_07 AC 77 ms
71,392 KB
testcase_08 AC 119 ms
77,872 KB
testcase_09 AC 133 ms
78,376 KB
testcase_10 AC 119 ms
77,972 KB
testcase_11 AC 225 ms
81,664 KB
testcase_12 AC 247 ms
81,680 KB
testcase_13 AC 263 ms
83,860 KB
testcase_14 AC 219 ms
80,320 KB
testcase_15 AC 249 ms
83,288 KB
testcase_16 AC 310 ms
88,068 KB
testcase_17 AC 250 ms
84,892 KB
testcase_18 AC 216 ms
80,808 KB
testcase_19 AC 210 ms
80,944 KB
testcase_20 AC 178 ms
78,580 KB
testcase_21 AC 263 ms
85,880 KB
testcase_22 AC 203 ms
87,640 KB
testcase_23 AC 193 ms
78,364 KB
testcase_24 AC 76 ms
71,384 KB
testcase_25 AC 353 ms
89,084 KB
testcase_26 AC 76 ms
71,468 KB
testcase_27 AC 75 ms
71,248 KB
testcase_28 AC 76 ms
71,556 KB
testcase_29 AC 76 ms
71,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#region Header
#!/usr/bin/env python3
# from typing import *

import sys
import io
import heapq

def input():
    return sys.stdin.readline()[:-1]

sys.setrecursionlimit(1000000)
#endregion

# _INPUT = """5 6 4 5 8
# 1 2 2
# 2 5 3
# 2 4 1
# 3 4 3
# 1 3 2
# 3 5 1
# """
# sys.stdin = io.StringIO(_INPUT)

def dijkstra(G, N, start):
    dist = [10**20 for _ in range(N)]
    hq = []
    heapq.heappush(hq, (0, start))
    dist[start] = 0
    while hq:
        d, p = heapq.heappop(hq)
        if d > dist[p]:
            continue
        for (p1, w1) in G[p]:
            d1 = dist[p] + w1
            if d1 < dist[p1]:
                dist[p1] = d1
                heapq.heappush(hq, (dist[p1], p1))
    return dist

def main():
    N, M, P, Q, T = map(int, input().split())
    P -= 1
    Q -= 1
    G = [list() for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        G[a-1].append((b-1, c))
        G[b-1].append((a-1, c))

    dist_0 = dijkstra(G, N, 0)
    dist_P = dijkstra(G, N, P)
    dist_Q = dijkstra(G, N, Q)

    if dist_0[P] + dist_P[Q] + dist_0[Q] <= T:
        print(T)
    
    else:
        max_t = -1
        for i in range(N):
            for j in range(i, N):
                # 0 -> i -> P -> j -> 0
                # 0 -> i -> Q -> j -> 0
                t_total1 = dist_0[i] + dist_P[i] + dist_P[j] + dist_0[j]
                t_total2 = dist_0[i] + dist_Q[i] + dist_Q[j] + dist_0[j]
                if t_total1 <= T and t_total2 <= T:
                    t = T - max(dist_P[i] + dist_P[j], dist_Q[i] + dist_Q[j])
                    max_t = max(max_t, t)
        print(max_t)


if __name__ == '__main__':
    main()
0