結果

問題 No.848 なかよし旅行
ユーザー square1001square1001
提出日時 2019-07-05 22:15:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 809 ms / 2,000 ms
コード長 1,513 bytes
コンパイル時間 775 ms
コンパイル使用メモリ 82,692 KB
実行使用メモリ 10,720 KB
最終ジャッジ日時 2024-04-25 13:49:34
合計ジャッジ時間 4,132 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 809 ms
10,720 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 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,944 KB
testcase_11 AC 33 ms
6,940 KB
testcase_12 AC 43 ms
6,940 KB
testcase_13 AC 60 ms
6,940 KB
testcase_14 AC 19 ms
6,940 KB
testcase_15 AC 58 ms
6,940 KB
testcase_16 AC 99 ms
8,448 KB
testcase_17 AC 68 ms
7,424 KB
testcase_18 AC 34 ms
6,940 KB
testcase_19 AC 29 ms
6,940 KB
testcase_20 AC 4 ms
6,944 KB
testcase_21 AC 82 ms
7,552 KB
testcase_22 AC 92 ms
7,296 KB
testcase_23 AC 11 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 107 ms
8,320 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const long long inf = 1LL << 61;
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;
}
vector<long long> dijkstra(vector<vector<edge> >& G, int src) {
	vector<long long> dist(G.size(), inf);
	dist[src] = 0;
	priority_queue<state> que;
	que.push(state{ src, 0 });
	while (!que.empty()) {
		int u = que.top().pos; que.pop();
		for (edge e : G[u]) {
			if (dist[e.to] > dist[u] + e.cost) {
				dist[e.to] = dist[u] + e.cost;
				que.push(state{ e.to, dist[e.to] });
			}
		}
	}
	return dist;
}
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<long long> s0 = dijkstra(G, 0);
	vector<long long> sp = dijkstra(G, P);
	vector<long long> sq = dijkstra(G, Q);
	if (max(sp[0] * 2, sq[0] * 2) > T) {
		cout << -1 << endl;
	}
	else if (s0[P] + sp[Q] + sq[0] <= T) {
		cout << T << endl;
	}
	else {
		long long ans = 0;
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) {
				long long sum = s0[i] + max(sp[i] + sp[j], sq[i] + sq[j]) + s0[j];
				if (sum <= T) {
					ans = max(ans, T - max(sp[i] + sp[j], sq[i] + sq[j]));
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}
0