結果

問題 No.848 なかよし旅行
ユーザー leaf_1415leaf_1415
提出日時 2019-07-07 03:20:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 146 ms / 2,000 ms
コード長 1,498 bytes
コンパイル時間 664 ms
コンパイル使用メモリ 74,748 KB
実行使用メモリ 10,592 KB
最終ジャッジ日時 2024-04-25 13:55:35
合計ジャッジ時間 2,904 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
10,592 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,948 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 39 ms
6,944 KB
testcase_12 AC 50 ms
6,940 KB
testcase_13 AC 70 ms
6,940 KB
testcase_14 AC 20 ms
6,940 KB
testcase_15 AC 62 ms
6,940 KB
testcase_16 AC 110 ms
8,192 KB
testcase_17 AC 71 ms
7,168 KB
testcase_18 AC 37 ms
6,940 KB
testcase_19 AC 34 ms
6,944 KB
testcase_20 AC 17 ms
6,944 KB
testcase_21 AC 91 ms
7,424 KB
testcase_22 AC 94 ms
7,296 KB
testcase_23 AC 22 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 119 ms
8,192 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <utility>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;

struct edge{
	llint to, cost;
	edge(){}
	edge(llint a, llint b){
		to = a, cost = b;
	}
};

llint n, m, p, q, t;
vector<edge> G[2005];
llint distS[2005], distP[2005], distQ[2005];

void dijkstra(llint S, llint dist[])
{
	for(int i = 1; i <= n; i++) dist[i] = inf;
	dist[S] = 0;
	
	priority_queue< P, vector<P>, greater<P> > Q;
	Q.push( make_pair(0, S) );
	
	llint v, d;
	while(Q.size()){
		d = Q.top().first;
		v = Q.top().second;
		Q.pop();
		if(dist[v] < d) continue;
		for(int i = 0; i < G[v].size(); i++){
			if(dist[G[v][i].to] > d + G[v][i].cost){
				dist[G[v][i].to] = d + G[v][i].cost;
				Q.push( make_pair(dist[G[v][i].to], G[v][i].to) );
			}
		}
	}
}

int main(void)
{
	cin >> n >> m >> p >> q >> t;
	llint u, v, w;
	for(int i = 1; i <= m; i++){
		cin >> u >> v >> w;
		G[u].push_back(edge(v, w));
		G[v].push_back(edge(u, w));
	}
	dijkstra(1, distS);
	dijkstra(p, distP);
	dijkstra(q, distQ);
	
	if(distS[p] + distP[q] + distQ[1] <= t){
		cout << t << endl;
		return 0;
	}
	
	llint ans = -1;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			llint spend = distS[i];
			spend += max(distP[i] + distP[j], distQ[i] + distQ[j]);
			spend += distS[j];
			if(spend > t) continue;
			ans = max(ans, t - max(distP[i] + distP[j], distQ[i] + distQ[j]));
		}
	}
	cout << ans << endl;
	
	return 0;
}
0