結果

問題 No.848 なかよし旅行
ユーザー tempura_pptempura_pp
提出日時 2019-03-16 01:11:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,214 bytes
コンパイル時間 1,769 ms
コンパイル使用メモリ 177,172 KB
実行使用メモリ 13,888 KB
最終ジャッジ日時 2023-08-07 19:39:16
合計ジャッジ時間 4,268 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 141 ms
13,888 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 32 ms
5,984 KB
testcase_12 AC 42 ms
6,784 KB
testcase_13 AC 57 ms
8,084 KB
testcase_14 AC 18 ms
4,452 KB
testcase_15 AC 55 ms
7,808 KB
testcase_16 AC 94 ms
11,072 KB
testcase_17 AC 66 ms
8,928 KB
testcase_18 AC 32 ms
5,748 KB
testcase_19 AC 27 ms
5,252 KB
testcase_20 AC 10 ms
4,380 KB
testcase_21 AC 82 ms
9,468 KB
testcase_22 AC 90 ms
10,384 KB
testcase_23 AC 21 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 103 ms
10,908 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
const ll longinf=1LL<<60 ;

using G=vector<vector<pair<ll,int>>>;
vector<ll> bfs(int s,G v){
	int n=v.size();
	vector<ll> dist(n,longinf);
	dist[s]=0;
	priority_queue<pair<ll,int>> que;
	que.emplace(0,s);
	while(que.size()){
		ll d=-que.top().first;
		int cur=que.top().second;
		que.pop();
		if(d>dist[cur])continue;
		for(auto e : v[cur]){
			if(dist[e.second]>d+e.first){
				dist[e.second]=d+e.first;
				que.emplace(-dist[e.second],e.second);
			}
		}
	}
	return dist;
}
	
int main(){
	int n,m,p,q,t;
	cin>>n>>m>>p>>q>>t;
	--p;--q;
	G v(n);
	rep(i,m){
		int a,b,c;
		cin>>a>>b>>c;
		--a;--b;
		v[a].emplace_back(c,b);
		v[b].emplace_back(c,a);
	}
	auto dist_s=bfs(0,v);
	auto dist_p=bfs(p,v);
	auto dist_q=bfs(q,v);
	ll ans=-1;
	ll ret=dist_s[p]+dist_p[q]+dist_q[0];
	if(ret<=t){
		cout<<t<<endl;
		return 0;
	}
	rep(i,n)rep(j,n){
		ll ret1=dist_s[i]+dist_p[i]+dist_p[j]+dist_s[j];
		ll ret2=dist_s[i]+dist_q[i]+dist_q[j]+dist_s[j];
		ll ret=max(ret1,ret2);
		if(ret<=t){
			ans=max(ans,t-ret+dist_s[i]+dist_s[j]);
		}
	}
	cout<<ans<<endl;
	return 0;
}
0