結果

問題 No.1308 ジャンプビーコン
ユーザー tkmst201tkmst201
提出日時 2021-01-05 18:21:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 737 ms / 4,000 ms
コード長 1,928 bytes
コンパイル時間 2,745 ms
コンパイル使用メモリ 214,852 KB
実行使用メモリ 74,496 KB
最終ジャッジ日時 2024-04-23 21:50:05
合計ジャッジ時間 10,972 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 6 ms
5,376 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 6 ms
5,376 KB
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 240 ms
74,368 KB
testcase_19 AC 240 ms
74,240 KB
testcase_20 AC 244 ms
74,240 KB
testcase_21 AC 240 ms
74,240 KB
testcase_22 AC 240 ms
74,240 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 348 ms
74,240 KB
testcase_27 AC 336 ms
74,240 KB
testcase_28 AC 339 ms
74,368 KB
testcase_29 AC 448 ms
74,496 KB
testcase_30 AC 445 ms
74,496 KB
testcase_31 AC 737 ms
74,368 KB
testcase_32 AC 473 ms
74,496 KB
testcase_33 AC 537 ms
74,496 KB
testcase_34 AC 338 ms
74,368 KB
testcase_35 AC 336 ms
74,240 KB
testcase_36 AC 168 ms
74,240 KB
testcase_37 AC 163 ms
74,368 KB
testcase_38 AC 336 ms
74,368 KB
testcase_39 AC 341 ms
74,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//

int main() {
	int N, Q, C;
	cin >> N >> Q >> C;
	vector<vector<pii>> g(N);
	REP(i, N - 1) {
		int u, v, l;
		scanf("%d %d %d", &u, &v, &l);
		--u; --v;
		g[u].emplace_back(v, l);
		g[v].emplace_back(u, l);
	}
	vector<int> X(Q);
	REP(i, Q) scanf("%d", &X[i]), --X[i];
	
	vector dist(N, vector<ll>(N));
	REP(i, N) {
		auto dfs = [&](auto self, int u, ll d, int par) -> void {
			dist[i][u] = d;
			for (auto [v, l] : g[u]) {
				if (v == par) continue;
				self(self, v, d + l, u);
			}
		};
		dfs(dfs, i, 0, -1);
	}
	
	vector<ll> dp(N), nextdp; // [i] := ビーコンの位置
	REP(i, N) dp[i] = dist[X[0]][i];
	REP(i, Q - 1) {
		// X[i] -> X[i + 1]
		nextdp.assign(N, longINF);
		REP(j, N) nextdp[j] = dp[j] + dist[X[i]][X[i + 1]]; // ビーコン設置なし
		queue<pii> que;
		que.emplace(X[i], -1);
		vector<int> vtx;
		while (!que.empty()) {
			auto [u, p] = que.front(); que.pop();
			vtx.emplace_back(u);
			for (auto [v, _] : g[u]) {
				if (v == p) continue;
				if (dist[X[i]][v] + dist[v][X[i + 1]] == dist[X[i]][X[i + 1]]) que.emplace(v, u);
			}
		}
		ll mn = *min_element(ALL(dp));
		REP(j, vtx.size()) {
			if (j > 0) mn += dist[vtx[j - 1]][vtx[j]];
			chmin(mn, dp[vtx[j]] + C);
			chmin(nextdp[vtx[j]], mn + dist[vtx[j]][X[i + 1]]);
		}
		
		swap(dp, nextdp);
	}
	cout << *min_element(ALL(dp)) << endl;
}
0