結果

問題 No.848 なかよし旅行
ユーザー 👑 jupirojupiro
提出日時 2020-01-16 08:10:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 2,310 bytes
コンパイル時間 1,296 ms
コンパイル使用メモリ 126,772 KB
実行使用メモリ 10,644 KB
最終ジャッジ日時 2024-04-25 14:11:10
合計ジャッジ時間 2,910 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
10,644 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 1 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,944 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,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 18 ms
6,940 KB
testcase_12 AC 23 ms
6,940 KB
testcase_13 AC 30 ms
6,944 KB
testcase_14 AC 12 ms
6,940 KB
testcase_15 AC 28 ms
6,940 KB
testcase_16 AC 46 ms
8,576 KB
testcase_17 AC 30 ms
7,552 KB
testcase_18 AC 17 ms
6,944 KB
testcase_19 AC 15 ms
6,940 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 37 ms
7,680 KB
testcase_22 AC 37 ms
7,424 KB
testcase_23 AC 19 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 49 ms
8,448 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 1 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; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

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

void dijkstra(ll start, vector<vector<pair<ll, 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<ll, ll>>> g(n);
	for (ll 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> d0(n, INF), dp(n, INF), dq(n, INF);
	dijkstra(0, g, d0);
	dijkstra(p, g, dp);
	dijkstra(q, g, dq);
	ll res = 0;
	if (d0[p] + dp[q] + dq[0] <= t) printf("%lld\n", t);
	else if (max(d0[p] + dp[0], d0[q] + dq[0]) > t) puts("-1");
	else {
		for (ll i = 0; i < n; i++) {
			for (ll j = 0; j < n; j++) {
				ll tmp = max(d0[i] + dp[i] + dp[j] + d0[j], d0[i] + dq[i] + dq[j] + d0[j]);
				if (tmp > t) continue;
				chmax(res, d0[i] + d0[j] + t - tmp);
			}
		}
		printf("%lld\n",res);
	}
	return 0;
}
0