結果
| 問題 |
No.848 なかよし旅行
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2019-07-12 04:09:17 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 677 ms / 2,000 ms |
| コード長 | 3,274 bytes |
| コンパイル時間 | 255 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 112,260 KB |
| 最終ジャッジ日時 | 2024-11-08 01:00:21 |
| 合計ジャッジ時間 | 8,844 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
N,M,P,Q,T=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(M)]
#グラフの重みつき最短距離(ダイクストラ法)
import heapq
#heapqは最初の要素が最小値になるデータ構造.
#第一要素に関して処理を行うので,最初の要素を最大値にしたい場合は(-x,x)に対してheapqすれば良い.
COST_vertex=[[] for i in range(N+1)]#iから他の点への辺とcostを列挙
for a,b,c in EDGE:
COST_vertex[a].append((b,c))
COST_vertex[b].append((a,c))#双方向の場合
MINCOST_1=[float("inf")]*(N+1)#求めたいリスト:startから頂点iへの最短距離
start=1
checking=[(0,start)]#start時点のcostは0.最初はこれをチェックする.
MINCOST_1[start]=0
while checking:
cost,checktown=heapq.heappop(checking)
#cost,checktownからの行き先をcheck.
#cost最小なものを確定し,確定したものはcheckingから抜く.
if MINCOST_1[checktown]<cost:#確定したものを再度チェックしなくて良い.
continue
for to,co in COST_vertex[checktown]:
if MINCOST_1[to]>cost+co:
MINCOST_1[to]=cost+co
#MINCOST候補に追加
heapq.heappush(checking,(cost+co,to))
#次にチェックする候補たちをcheckingに入れる.
MINCOST_P=[float("inf")]*(N+1)#求めたいリスト:startから頂点iへの最短距離
start=P
checking=[(0,start)]#start時点のcostは0.最初はこれをチェックする.
MINCOST_P[start]=0
while checking:
cost,checktown=heapq.heappop(checking)
#cost,checktownからの行き先をcheck.
#cost最小なものを確定し,確定したものはcheckingから抜く.
if MINCOST_P[checktown]<cost:#確定したものを再度チェックしなくて良い.
continue
for to,co in COST_vertex[checktown]:
if MINCOST_P[to]>cost+co:
MINCOST_P[to]=cost+co
#MINCOST候補に追加
heapq.heappush(checking,(cost+co,to))
#次にチェックする候補たちをcheckingに入れる.
MINCOST_Q=[float("inf")]*(N+1)#求めたいリスト:startから頂点iへの最短距離
start=Q
checking=[(0,start)]#start時点のcostは0.最初はこれをチェックする.
MINCOST_Q[start]=0
while checking:
cost,checktown=heapq.heappop(checking)
#cost,checktownからの行き先をcheck.
#cost最小なものを確定し,確定したものはcheckingから抜く.
if MINCOST_Q[checktown]<cost:#確定したものを再度チェックしなくて良い.
continue
for to,co in COST_vertex[checktown]:
if MINCOST_Q[to]>cost+co:
MINCOST_Q[to]=cost+co
#MINCOST候補に追加
heapq.heappush(checking,(cost+co,to))
#次にチェックする候補たちをcheckingに入れる.
if MINCOST_1[P]+MINCOST_P[Q]+MINCOST_Q[1]<=T:
print(T)
else:
ANS=-1
for i in range(1,N+1):
for j in range(1,N+1):
if MINCOST_1[i] + MINCOST_1[j] + max(MINCOST_P[i]+MINCOST_P[j],MINCOST_Q[i]+MINCOST_Q[j]) <=T:
ANS=max(ANS,T - max(MINCOST_P[i]+MINCOST_P[j],MINCOST_Q[i]+MINCOST_Q[j]))
if ANS==-1:
print(-1)
else:
print(ANS)
titia