結果

問題 No.848 なかよし旅行
ユーザー square1001square1001
提出日時 2019-07-05 22:05:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,272 bytes
コンパイル時間 960 ms
コンパイル使用メモリ 85,336 KB
実行使用メモリ 52,796 KB
最終ジャッジ日時 2024-04-16 07:23:39
合計ジャッジ時間 7,589 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const long long inf = 1LL << 62;
struct edge {
	int to; long long cost;
};
struct state {
	int pos; long long cost;
};
bool operator<(const state& s1, const state& s2) {
	return s1.cost > s2.cost;
}
int main() {
	int N, M, P, Q; long long T;
	cin >> N >> M >> P >> Q >> T; --P, --Q;
	vector<vector<edge> > G(N);
	for (int i = 0; i < M; ++i) {
		int a, b; long long c;
		cin >> a >> b >> c; --a, --b;
		G[a].push_back(edge{ b, c });
		G[b].push_back(edge{ a, c });
	}
	vector<vector<long long> > dist(N, vector<long long>(N, inf));
	for (int i = 0; i < N; ++i) {
		dist[i][i] = 0;
		priority_queue<state> que;
		que.push(state{ i, 0 });
		while (!que.empty()) {
			int u = que.top().pos; que.pop();
			for (edge e : G[u]) {
				if (dist[i][e.to] > dist[i][u] + e.cost) {
					dist[i][e.to] = dist[i][u] + e.cost;
					que.push(state{ e.to, dist[i][e.to] });
				}
			}
		}
	}
	if (dist[0][P] + dist[P][Q] + dist[Q][0] <= T) {
		cout << T << endl;
	}
	else {
		long long ans = -1;
		for (int i = 0; i < N; ++i) {
			long long div = max(dist[i][P], dist[i][Q]) * 2;
			if (div + dist[0][i] * 2 <= T) {
				ans = max(ans, T - div);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
0