結果

問題 No.848 なかよし旅行
ユーザー hedwig100hedwig100
提出日時 2020-04-17 16:17:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 451 ms / 2,000 ms
コード長 1,356 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 82,132 KB
実行使用メモリ 102,764 KB
最終ジャッジ日時 2024-04-25 14:15:50
合計ジャッジ時間 5,765 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 451 ms
102,764 KB
testcase_01 AC 40 ms
54,312 KB
testcase_02 AC 38 ms
54,388 KB
testcase_03 AC 37 ms
53,784 KB
testcase_04 AC 37 ms
55,128 KB
testcase_05 AC 38 ms
54,988 KB
testcase_06 AC 51 ms
64,196 KB
testcase_07 AC 38 ms
54,708 KB
testcase_08 AC 89 ms
76,280 KB
testcase_09 AC 103 ms
76,936 KB
testcase_10 AC 88 ms
76,816 KB
testcase_11 AC 186 ms
80,240 KB
testcase_12 AC 213 ms
81,152 KB
testcase_13 AC 218 ms
82,412 KB
testcase_14 AC 160 ms
78,840 KB
testcase_15 AC 218 ms
82,284 KB
testcase_16 AC 279 ms
86,932 KB
testcase_17 AC 212 ms
83,180 KB
testcase_18 AC 183 ms
79,884 KB
testcase_19 AC 177 ms
79,088 KB
testcase_20 AC 144 ms
77,516 KB
testcase_21 AC 239 ms
84,852 KB
testcase_22 AC 189 ms
86,600 KB
testcase_23 AC 160 ms
77,192 KB
testcase_24 AC 36 ms
52,820 KB
testcase_25 AC 306 ms
87,332 KB
testcase_26 AC 36 ms
52,684 KB
testcase_27 AC 37 ms
54,500 KB
testcase_28 AC 37 ms
53,292 KB
testcase_29 AC 38 ms
54,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7
INF = 10 ** 12
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from  itertools import permutations
from heapq import heapify,heappop,heappush

def dijkstra(G,s = 0): #sを始点とした最短経を求める
    n = len(G)
    dist = [INF] * n
    dist[s] = 0

    q = [(0,s)]
    heapify(q)

    while q:
        d,v = heappop(q)
        if dist[v] < d:
            continue
        for e,cost in G[v]:
            if dist[e] > dist[v] + cost:
                dist[e] = dist[v] + cost
                heappush(q,(dist[e],e))
    
    return dist

def main():
    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((b,c))
        G[b].append((a,c))
    
    d0 = dijkstra(G)
    dp = dijkstra(G,p)
    dq = dijkstra(G,q)
    if d0[p] + dp[q] + dq[0] <= t:
        print(t)
        return

    ans = -1
    for i in range(n):
        if 2 * (d0[i] + dp[i] + dq[i]) <= t:
            print(t)
            return
    
    for i in range(n):
        for j in range(n):
            if d0[i] + d0[j] + max(dp[i] + dp[j],dq[i] + dq[j]) <= t:
                ans = max(ans,t - max(dp[i] + dp[j],dq[i] + dq[j]))
    print(ans)

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