记录编号 349910 评测结果 WAAAAAAAAAAAAAAAAAAA
题目名称 树和机器人 最终得分 95
用户昵称 GravatarBIRD 是否通过 未通过
代码语言 C++ 运行时间 3.944 s
提交时间 2016-11-15 13:08:30 内存使用 198.71 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;

int n, root, total, K;
int head[500100];
ll dp[500100][50];

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

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;
}