結果

問題 No.848 なかよし旅行
ユーザー leaf1415leaf1415
提出日時 2019-07-05 22:05:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 1,498 bytes
コンパイル時間 613 ms
コンパイル使用メモリ 73,304 KB
実行使用メモリ 10,544 KB
最終ジャッジ日時 2023-08-07 19:40:27
合計ジャッジ時間 3,290 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 148 ms
10,544 KB
testcase_01 AC 2 ms
4,380 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 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 39 ms
5,196 KB
testcase_12 AC 51 ms
5,556 KB
testcase_13 AC 67 ms
6,460 KB
testcase_14 AC 22 ms
4,376 KB
testcase_15 AC 62 ms
6,424 KB
testcase_16 AC 105 ms
8,216 KB
testcase_17 AC 69 ms
7,220 KB
testcase_18 AC 38 ms
5,228 KB
testcase_19 AC 35 ms
4,804 KB
testcase_20 AC 20 ms
4,376 KB
testcase_21 AC 86 ms
7,496 KB
testcase_22 AC 88 ms
7,376 KB
testcase_23 AC 26 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 113 ms
8,032 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 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