結果

問題 No.848 なかよし旅行
ユーザー chocoruskchocorusk
提出日時 2019-01-25 17:49:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,462 bytes
コンパイル時間 880 ms
コンパイル使用メモリ 103,556 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2024-04-25 13:46:58
合計ジャッジ時間 3,333 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
10,496 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 33 ms
6,940 KB
testcase_12 AC 45 ms
6,944 KB
testcase_13 AC 61 ms
6,940 KB
testcase_14 AC 18 ms
6,944 KB
testcase_15 AC 55 ms
6,944 KB
testcase_16 AC 99 ms
8,192 KB
testcase_17 AC 68 ms
7,296 KB
testcase_18 AC 33 ms
6,944 KB
testcase_19 AC 29 ms
6,944 KB
testcase_20 AC 5 ms
6,944 KB
testcase_21 AC 84 ms
7,296 KB
testcase_22 AC 92 ms
7,424 KB
testcase_23 AC 10 ms
6,944 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 102 ms
8,192 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 1 ms
6,944 KB
testcase_29 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef pair<ll, int> P;
const ll INF=1e16;
int n, m, p, q;
vector<P> g[2001];
void dijkstra(int s, ll d[2001]){
	fill(d, d+n, INF);
	d[s]=0;
	priority_queue<P, vector<P>, greater<P>> que;
	que.push(P(0, s));
	while(!que.empty()){
		P p1=que.top(); que.pop();
		int x=p1.second;
		if(p1.first>d[x]) continue;
		for(int j=0; j<g[x].size(); j++){
			P q1=g[x][j];
			int y=q1.second;
			if(d[y]>d[x]+q1.first){
				d[y]=d[x]+q1.first;
				que.push(P(d[y], y));
			}
		}
	}
}

int main()
{
	ll t;
	cin>>n>>m>>p>>q>>t; p--; q--;
	for(int i=0; i<n; i++) g[i].clear();
	for(int i=0; i<m; i++){
		int a, b; ll c; cin>>a>>b>>c; a--; b--;
		g[a].push_back(P(c, b));
		g[b].push_back(P(c, a));
	}
	ll d1[2001], dp[2001], dq[2001];
	dijkstra(0, d1);
	dijkstra(p, dp);
	dijkstra(q, dq);
	if(t<max(2*d1[p], 2*d1[q])){
		cout<<-1<<endl;
		return 0;
	}else if(t>=d1[p]+dp[q]+d1[q]){
		cout<<t<<endl;
		return 0;
	}
	ll ans=0;
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			ll dm=max(dp[i]+dp[j], dq[i]+dq[j]);
			if(dm+d1[i]+d1[j]>t) continue;
			ans=max(ans, t-dm);
		}
	}
	cout<<ans<<endl;
	return 0;
}
0