比赛 20161115 评测结果 RRRRRRRRRRRRTTTTTTTT
题目名称 树和机器人 最终得分 0
用户昵称 BIRD 运行时间 8.043 s
代码语言 C++ 内存使用 1.76 MiB
提交时间 2016-11-15 11:58:23
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;

int n, root, total, K;
int head[10010];
ll dp[10010][18];

struct Edge {
        int v, w, next;
}edge[20010];

void AddEdge(int u, int v, int w)
{
        edge[total].v    = v;
        edge[total].w    = w;
        edge[total].next = head[u];
        head[u] = total++;
}

void tiny_bird(int u, int p)
{
    for (int id = head[u]; id != -1; id = edge[id].next) {
        int v = edge[id].v, w = edge[id].w;
        if (v == p) continue;
        tiny_bird(v, u);
        for (int i = K; i >= 0; --i) {
            dp[u][i] += dp[v][0] + 2 * w;
            for (int j = 1; j <= i; ++j) {
                dp[u][i] = min(dp[u][i], dp[v][j] + dp[u][i - j] + j * w);
            }
        }
    }
}

int main()
{
        freopen("trobot.in","r",stdin);
 //       freopen("trobot.out","w",stdout);
        while(~scanf("%d%d%d", &n, &root, &K)){
        memset(head, -1, sizeof(head));
        total = 0;
        for (int i = 1; i < n; ++i) {
                int u, v, w;
                scanf("%d%d%d", &u, &v, &w);
                AddEdge(u, v, w);
                AddEdge(v, u, w);
        }
        memset(dp,0,sizeof(dp));
        tiny_bird(root,1);
        printf("%lld\n",dp[root][K]);
        }
        return 0;
}