比赛 |
20161115 |
评测结果 |
AAAAAAAAAAAAAAAAAAAA |
题目名称 |
树和机器人 |
最终得分 |
100 |
用户昵称 |
KZNS |
运行时间 |
0.305 s |
代码语言 |
C++ |
内存使用 |
1.02 MiB |
提交时间 |
2016-11-15 11:27:40 |
显示代码纯文本
//KZNS
#include <cstdio>
#include <vector>
#include <utility>
#include <cstring>
using namespace std;
#define Nmax 50004
#define Kmax 23
typedef pair<int, int> pr;
vector<pr> gp[Nmax];
int N, R, K;
void init() {
scanf("%d %d %d", &N, &R, &K);
int a, b, c;
for (int i = 1; i < N; i++) {
scanf("%d %d %d", &a, &b, &c);
gp[a].push_back(make_pair(b, c));
gp[b].push_back(make_pair(a, c));
}
}
int fa[Nmax];
void DFS(int x, int *F) {
int v;
int mn;
int G[Kmax];
for (int i = 0; i < gp[x].size(); i++) {
if (fa[x] == gp[x][i].first)
continue;
memset(G, 0, sizeof(G));
fa[gp[x][i].first] = x;
DFS(gp[x][i].first, G);
v = gp[x][i].second;
for (int k = K; k >= 0; k--) {
mn = F[k] + G[0] + v*2;
for (int g = 1; g <= k; g++)
mn = min(mn, F[k-g] + G[g] + g * v);
F[k] = mn;
}
//for (int k = 1; k <= K; k++)
// F[k] = min(F[k], F[k-1]);
}
}
int main() {
freopen("trobot.in", "r", stdin);
freopen("trobot.out", "w", stdout);
init();
int F[Kmax] = {0};
DFS(R, F);
printf("%d\n", F[K]);
return 0;
}
//UBWH