結果

問題 No.848 なかよし旅行
ユーザー AEnAEn
提出日時 2022-12-01 11:02:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 489 ms / 2,000 ms
コード長 1,105 bytes
コンパイル時間 133 ms
コンパイル使用メモリ 82,316 KB
実行使用メモリ 103,456 KB
最終ジャッジ日時 2024-04-17 02:05:06
合計ジャッジ時間 7,075 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 489 ms
103,456 KB
testcase_01 AC 36 ms
53,888 KB
testcase_02 AC 31 ms
53,276 KB
testcase_03 AC 33 ms
53,376 KB
testcase_04 AC 33 ms
53,504 KB
testcase_05 AC 33 ms
53,376 KB
testcase_06 AC 49 ms
66,304 KB
testcase_07 AC 35 ms
53,504 KB
testcase_08 AC 81 ms
76,756 KB
testcase_09 AC 104 ms
77,544 KB
testcase_10 AC 83 ms
76,540 KB
testcase_11 AC 274 ms
81,804 KB
testcase_12 AC 308 ms
82,820 KB
testcase_13 AC 281 ms
83,768 KB
testcase_14 AC 249 ms
80,208 KB
testcase_15 AC 285 ms
84,516 KB
testcase_16 AC 337 ms
89,104 KB
testcase_17 AC 276 ms
84,520 KB
testcase_18 AC 254 ms
80,960 KB
testcase_19 AC 238 ms
80,944 KB
testcase_20 AC 127 ms
78,336 KB
testcase_21 AC 301 ms
86,372 KB
testcase_22 AC 234 ms
87,688 KB
testcase_23 AC 191 ms
78,336 KB
testcase_24 AC 35 ms
52,736 KB
testcase_25 AC 382 ms
89,344 KB
testcase_26 AC 35 ms
52,992 KB
testcase_27 AC 35 ms
52,864 KB
testcase_28 AC 36 ms
53,120 KB
testcase_29 AC 36 ms
52,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush
INF = float('inf')

def dijkstra(s, n):
    dist = [INF] * n
    hq = [(0, s)] # (distance, node)
    dist[s] = 0
    seen = [False] * n
    while hq:
        dis, v = heappop(hq)
        if dist[v] < dis:
            continue
        seen[v] = True
        for to, cost in G[v]:
            if seen[to] == False and dist[v] + cost < dist[to]:
                dist[to] = dist[v] + cost
                heappush(hq, (dist[to], to))
    return dist

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

dis_0 = dijkstra(0,N)
dis_p = dijkstra(P-1,N)
dis_q = dijkstra(Q-1,N)
if max(dis_p[0]*2,dis_q[0]*2)>T:
    exit(print(-1))

if 2*min(dis_p[0],dis_q[0])+2*dis_p[Q-1]<=T or dis_p[0]+dis_q[0]+dis_p[Q-1]<=T:
    exit(print(T))

res = 0
for i in range(N):
    for j in range(N):
        if dis_0[i]+dis_0[j]+max(dis_p[i]+dis_p[j],dis_q[i]+dis_q[j])<=T:
            res = max(res,T-max(dis_p[i]+dis_p[j],dis_q[i]+dis_q[j]))
print(res)
0