比赛 20161115 评测结果 WWTTTTTTWWWWTTTTTTTT
题目名称 树和机器人 最终得分 0
用户昵称 Rapiz 运行时间 15.493 s
代码语言 C++ 内存使用 11.15 MiB
提交时间 2016-11-15 11:30:38
显示代码纯文本
#include <cstdio>
#include <algorithm>
#include <cstring>
#define file(x) "trobot." #x
using std::min;
const int V = 5e4 + 10, K = 25, E = V << 1;
struct EDGE{int u, v, w;}st[E];
int n, r, k, hed[V], nxt[E], f[V][K];
inline void _add(int u, int v, int w) {
	static int sz = 0;
	st[++sz].u = u, st[sz].v = v, st[sz].w = w;
	nxt[sz] = hed[u], hed[u] = sz;
}
void add(int u, int v, int w) {
	_add(u, v, w), _add(v, u, w);
}
int g[V][K], pre[V];
void dfs(int u, int fa) {
	int cnt = 0;
	for (int e = hed[u], v = st[e].v; e; v = st[e = nxt[e]].v) if(v != fa) {
		pre[++cnt] = st[e].w;
		dfs(v, u);
	}
	if (!cnt) {
		for (int j = 1; j <= k; j++) f[u][j] = 0;
		return;
	}
	std::sort(pre + 1, pre + 1 + cnt);
	for (int i = 1; i <= cnt; i++) pre[i] += pre[i - 1];
	memset(g, 0x3f, sizeof(g));
	for (int i = 0; i <= k; i++) g[0][i] = 0;
	for (int e = hed[u], v = st[e].v, i = 1; e; v = st[e = nxt[e]].v, i++) if (v != fa) {
		for (int j = 1; j <= k; j++) {
			for (int p = 1; p <= j; p++)
				for (int q = 0; q <= p; q++) g[i][j] = min(g[i][j], g[i - 1][j - p + q] + p*st[e].w + pre[q] + f[v][p]);
		}
	}
	for (int j = 1; j <= k; j++) f[u][j] = g[cnt][j];
}
int main() {
	freopen(file(in), "r", stdin);
	freopen(file(out), "w", stdout);
	memset(f, 0x3f, sizeof(f));
	scanf("%d%d%d", &n, &r, &k);
	for (int i = 1; i < n; i++) {
		int a, b, v;scanf("%d%d%d", &a, &b, &v);
		add(a, b, v);
	}
	dfs(r, -1);
	printf("%d", f[r][k]);
}