結果

問題 No.848 なかよし旅行
ユーザー A8pfA8pf
提出日時 2019-07-22 13:30:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 1,368 bytes
コンパイル時間 812 ms
コンパイル使用メモリ 84,568 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-04-25 13:56:57
合計ジャッジ時間 2,918 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 151 ms
10,752 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,948 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 34 ms
6,944 KB
testcase_12 AC 46 ms
6,940 KB
testcase_13 AC 63 ms
6,948 KB
testcase_14 AC 19 ms
6,944 KB
testcase_15 AC 58 ms
6,944 KB
testcase_16 AC 104 ms
8,320 KB
testcase_17 AC 73 ms
7,424 KB
testcase_18 AC 34 ms
6,940 KB
testcase_19 AC 30 ms
6,944 KB
testcase_20 AC 11 ms
6,940 KB
testcase_21 AC 87 ms
7,552 KB
testcase_22 AC 96 ms
7,552 KB
testcase_23 AC 11 ms
6,940 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 111 ms
8,320 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
class Edge
{
	public:
	int to;
	ll cost;
	Edge(int t,ll c)
	{
		to=t;
		cost=c;
	}
};

vector<Edge> g[2000];
ll d[3][2000];//0...0から 1...pから 2...qから

void dijk(int s,int n,int mode)
{
	priority_queue<P,vector<P>,greater<P>> q;
	d[mode][s]=0;
	q.push(P(0,s));
	while(!q.empty())
	{
		P p=q.top();q.pop();
		if(p.first>d[mode][p.second])
			continue;
		for(int i=0;i<g[p.second].size();i++)
		{
			Edge e=g[p.second][i];
			if(p.first+e.cost<d[mode][e.to])
			{
				d[mode][e.to]=p.first+e.cost;
				q.push(P(d[mode][e.to],e.to));
			}
		}
	}
	return;
}
int main()
{
	int n,m,p,q,t;
	ll ans=-1;
	cin>>n>>m>>p>>q>>t;
	p--;q--;
	fill(d[0],d[3],1e15-1);
	for(int i=0;i<m;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		a--;b--;
		g[a].push_back(Edge(b,c));
		g[b].push_back(Edge(a,c));
	}
	dijk(0,n,0);
	dijk(p,n,1);
	dijk(q,n,2);
	
	//中間ノード2つの位置を全探索
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(d[0][i]+max(d[1][i]+d[1][j],d[2][i]+d[2][j])+d[0][j]<=t)
				ans=max(ans,t-max(d[1][i]+d[1][j],d[2][i]+d[2][j]));
		}
	}
	//全部通って間に合う場合を考える
	if(min(d[0][p]+d[1][q]+d[2][0],d[0][q]+d[2][p]+d[1][0])<=t)
		ans=t;
	cout<<ans<<endl;
	return 0;
}
0