結果

問題 No.848 なかよし旅行
ユーザー chocoruskchocorusk
提出日時 2021-03-16 02:40:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 968 ms / 2,000 ms
コード長 1,463 bytes
コンパイル時間 1,213 ms
コンパイル使用メモリ 128,100 KB
実行使用メモリ 10,856 KB
最終ジャッジ日時 2024-04-25 14:24:34
合計ジャッジ時間 5,147 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 968 ms
10,856 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 4 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 34 ms
6,940 KB
testcase_12 AC 43 ms
6,940 KB
testcase_13 AC 58 ms
6,940 KB
testcase_14 AC 17 ms
6,944 KB
testcase_15 AC 55 ms
6,940 KB
testcase_16 AC 98 ms
8,320 KB
testcase_17 AC 68 ms
7,296 KB
testcase_18 AC 33 ms
6,944 KB
testcase_19 AC 28 ms
6,944 KB
testcase_20 AC 4 ms
6,940 KB
testcase_21 AC 85 ms
7,552 KB
testcase_22 AC 93 ms
7,552 KB
testcase_23 AC 10 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 108 ms
8,320 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 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