結果

問題 No.848 なかよし旅行
ユーザー 👑 jupirojupiro
提出日時 2020-03-17 05:53:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 2,558 bytes
コンパイル時間 1,575 ms
コンパイル使用メモリ 129,100 KB
実行使用メモリ 10,768 KB
最終ジャッジ日時 2024-04-25 14:13:53
合計ジャッジ時間 3,149 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
10,768 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 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,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 19 ms
6,944 KB
testcase_12 AC 24 ms
6,944 KB
testcase_13 AC 32 ms
6,944 KB
testcase_14 AC 13 ms
6,940 KB
testcase_15 AC 29 ms
6,940 KB
testcase_16 AC 48 ms
8,448 KB
testcase_17 AC 33 ms
7,424 KB
testcase_18 AC 18 ms
6,944 KB
testcase_19 AC 16 ms
6,940 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 39 ms
7,552 KB
testcase_22 AC 38 ms
7,424 KB
testcase_23 AC 24 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 55 ms
8,576 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

void dijkstra(ll start, vector<vector<pair<int, ll>>>& graph, vector<ll>& dist) {
	dist[start] = 0;
	priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
	vector<bool> used(dist.size(), false);
	pq.push(make_pair(0, start));
	while (!pq.empty()) {
		ll d, node;
		tie(d, node) = pq.top();
		pq.pop();
		if (used[node]) continue;
		used[node] = true;
		for (pair<ll, ll> element : graph[node]) {
			ll new_d, new_node;
			tie(new_node, new_d) = element;
			new_d += d;
			if (new_d < dist[new_node]) {
				dist[new_node] = new_d;
				pq.push(make_pair(dist[new_node], new_node));
			}
		}
	}
}

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	ll N, M, P, Q, T; scanf("%lld %lld %lld %lld %lld", &N, &M, &P, &Q, &T);
	P--; Q--;
	vector<vector<pair<int, ll>>> g(N);
	for (int i = 0; i < M; i++) {
		ll a, b, c; scanf("%lld %lld %lld", &a, &b, &c);
		a--; b--;
		g[a].emplace_back(b, c);
		g[b].emplace_back(a, c);
	}
	vector<ll> dist1(N, INF), dist2(N, INF), dist3(N, INF);
	dijkstra(0, g, dist1);
	dijkstra(P, g, dist2);
	dijkstra(Q, g, dist3);
	if (dist1[P] + dist2[Q] + dist1[Q] <= T) {
		cout << T << endl;
		return 0;
	}
	if (max(dist1[P] * 2, dist1[Q] * 2) > T) {
		puts("-1");
		return 0;
	}
	ll res = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			ll t1 = dist1[i] + dist2[i] + dist2[j] + dist1[j];
			ll t2 = dist1[i] + dist3[i] + dist3[j] + dist1[j];
			if (max(t1, t2) > T) continue;
			chmax(res, T - max(dist2[i]+dist2[j],dist3[i]+dist3[j]));
		}
	}
	cout << res << endl;
	return 0;
	/*
		おまじないを使ったらscanfとprintf関連注意!!!!!!!!!!!!
	*/
}
0