結果

問題 No.848 なかよし旅行
ユーザー furafura
提出日時 2020-06-01 05:00:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 1,361 bytes
コンパイル時間 2,190 ms
コンパイル使用メモリ 210,528 KB
実行使用メモリ 10,696 KB
最終ジャッジ日時 2023-08-07 20:02:12
合計ジャッジ時間 4,208 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
10,696 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 25 ms
5,428 KB
testcase_12 AC 31 ms
5,824 KB
testcase_13 AC 39 ms
6,740 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 36 ms
6,500 KB
testcase_16 AC 57 ms
8,300 KB
testcase_17 AC 35 ms
7,420 KB
testcase_18 AC 23 ms
5,188 KB
testcase_19 AC 23 ms
4,804 KB
testcase_20 AC 16 ms
4,376 KB
testcase_21 AC 43 ms
7,408 KB
testcase_22 AC 38 ms
7,640 KB
testcase_23 AC 24 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 56 ms
8,452 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

template<class T> struct edge{
	int to;
	T wt;
	edge(int to,const T& wt):to(to),wt(wt){}
};
template<class T> using weighted_graph=vector<vector<edge<T>>>;

template<class T>
void add_undirected_edge(weighted_graph<T>& G,int u,int v,const T& wt){
	G[u].emplace_back(v,wt);
	G[v].emplace_back(u,wt);
}

template<class T>
vector<T> Dijkstra(const weighted_graph<T>& G,int s){
	const T INF=numeric_limits<T>::max();
	int n=G.size();
	vector<T> d(n,INF); d[s]=0;
	priority_queue<pair<T,int>> Q; Q.emplace(0,s);
	while(!Q.empty()){
		T d0=-Q.top().first;
		int u=Q.top().second; Q.pop();
		if(d0>d[u]) continue;
		for(const auto& e:G[u]){
			int v=e.to;
			if(d[v]>d[u]+e.wt) d[v]=d[u]+e.wt, Q.emplace(-d[v],v);
		}
	}
	return d;
}

int main(){
	int n,m,p,q,T; scanf("%d%d%d%d%d",&n,&m,&p,&q,&T); p--; q--;
	weighted_graph<lint> G(n);
	rep(i,m){
		int u,v;
		lint c; scanf("%d%d%lld",&u,&v,&c); u--; v--;
		add_undirected_edge(G,u,v,c);
	}

	auto d1=Dijkstra(G,0);
	auto d2=Dijkstra(G,p);
	auto d3=Dijkstra(G,q);

	if(d1[p]+d2[q]+d3[0]<=T){
		printf("%d\n",T);
		return 0;
	}

	lint ans=-1;
	rep(u,n) rep(v,n) {
		if(d1[u]+d1[v]+max(d2[u]+d2[v],d3[u]+d3[v])<=T){
			ans=max(ans,T-max(d2[u]+d2[v],d3[u]+d3[v]));
		}
	}
	printf("%lld\n",ans);

	return 0;
}
0