Submission #1261273

#TimeUsernameProblemLanguageResultExecution timeMemory
1261273kaiboyMuseum (CEOI17_museum)C++20
100 / 100
228 ms180756 KiB
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int   N = 10000;
const int INF = 0x3f3f3f3f;

int ij[N], ww[N], sz[N], dp[N][N + 1][2];
vector<int> eh[N];

int dfs(int h_, int i) {
	int s_ = 1;
	for (int h : eh[i])
		if (h != h_) {
			int j = i ^ ij[h], s = dfs(h, j);
			for (int k = s_ + s; k; k--)
				for (int f = 0; f < 2; f++)
					if (k > s_)
						dp[i][k][f] = INF;
					else
						for (int l = 1; l <= s; l++)
							for (int g = 0; f + g < 2; g++)
								dp[i][k + l][f + g] = min(dp[i][k + l][f + g], dp[i][k][f] + dp[j][l][g]);
			s_ += s;
		}
	if (h_ != -1)
		for (int k = 1; k <= s_; k++) {
			dp[i][k][0] += ww[h_] * 2;
			dp[i][k][1] += ww[h_];
		}
	return s_;
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(NULL);
	int n, k, r; cin >> n >> k >> r, r--;
	for (int h = 0; h < n - 1; h++) {
		int i, j, w; cin >> i >> j >> w, i--, j--;
		ij[h] = i ^ j, ww[h] = w;
		eh[i].push_back(h);
		eh[j].push_back(h);
	}
	dfs(-1, r);
	cout << dp[r][k][1] << '\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...