結果

問題 No.848 なかよし旅行
ユーザー tcltktcltk
提出日時 2021-01-31 05:05:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 491 ms / 2,000 ms
コード長 1,748 bytes
コンパイル時間 485 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 102,016 KB
最終ジャッジ日時 2024-11-08 01:22:51
合計ジャッジ時間 6,783 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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