結果

問題 No.848 なかよし旅行
ユーザー tcltktcltk
提出日時 2021-01-31 05:05:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 492 ms / 2,000 ms
コード長 1,748 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 101,868 KB
最終ジャッジ日時 2024-04-25 14:23:53
合計ジャッジ時間 6,210 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 492 ms
101,868 KB
testcase_01 AC 43 ms
53,704 KB
testcase_02 AC 41 ms
53,636 KB
testcase_03 AC 42 ms
54,236 KB
testcase_04 AC 39 ms
53,268 KB
testcase_05 AC 43 ms
53,972 KB
testcase_06 AC 52 ms
62,296 KB
testcase_07 AC 41 ms
54,140 KB
testcase_08 AC 89 ms
76,960 KB
testcase_09 AC 103 ms
77,016 KB
testcase_10 AC 88 ms
76,384 KB
testcase_11 AC 202 ms
79,964 KB
testcase_12 AC 213 ms
81,056 KB
testcase_13 AC 245 ms
82,200 KB
testcase_14 AC 194 ms
78,492 KB
testcase_15 AC 231 ms
82,148 KB
testcase_16 AC 295 ms
87,664 KB
testcase_17 AC 236 ms
82,992 KB
testcase_18 AC 191 ms
79,464 KB
testcase_19 AC 185 ms
79,092 KB
testcase_20 AC 150 ms
77,776 KB
testcase_21 AC 258 ms
84,632 KB
testcase_22 AC 178 ms
85,676 KB
testcase_23 AC 168 ms
77,388 KB
testcase_24 AC 39 ms
53,064 KB
testcase_25 AC 331 ms
87,012 KB
testcase_26 AC 38 ms
52,816 KB
testcase_27 AC 39 ms
53,412 KB
testcase_28 AC 40 ms
53,444 KB
testcase_29 AC 40 ms
54,180 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