結果

問題 No.1308 ジャンプビーコン
ユーザー tkmst201tkmst201
提出日時 2021-01-05 18:19:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,929 bytes
コンパイル時間 2,932 ms
コンパイル使用メモリ 213,072 KB
実行使用メモリ 39,168 KB
最終ジャッジ日時 2024-04-23 21:46:55
合計ジャッジ時間 9,994 ms
ジャッジサーバーID
(参考情報)
judge5 / 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 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 6 ms
5,376 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 213 ms
39,040 KB
testcase_22 AC 212 ms
39,040 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 295 ms
39,040 KB
testcase_27 AC 295 ms
38,912 KB
testcase_28 AC 291 ms
38,912 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 301 ms
38,912 KB
testcase_35 AC 295 ms
38,912 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

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<int>(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