HDU3452 Bonsai(树形DP)

21 篇文章 0 订阅
6 篇文章 0 订阅

Bonsai

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 877    Accepted Submission(s): 435


Problem Description
After being assaulted in the parking lot by Mr. Miyagi following the "All Valley Karate Tournament", John Kreese has come to you for assistance. Help John in his quest for justice by chopping off all the leaves from Mr. Miyagi's bonsai tree!
You are given an undirected tree (i.e., a connected graph with no cycles), where each edge (i.e., branch) has a nonnegative weight (i.e., thickness). One vertex of the tree has been designated the root of the tree.The remaining vertices of the tree each have unique paths to the root; non-root vertices which are not the successors of any other vertex on a path to the root are known as leaves.Determine the minimum weight set of edges that must be removed so that none of the leaves in the original tree are connected by some path to the root.
 

Input
The input file will contain multiple test cases. Each test case will begin with a line containing a pair of integers n (where 1 <= n <= 1000) and r (where r ∈ {1,……, n}) indicating the number of vertices in the tree and the index of the root vertex, respectively. The next n-1 lines each contain three integers u i v i w i (where u i, v i ∈ {1,……, n} and 0 <= w i <= 1000) indicating that vertex u i is connected to vertex v i by an undirected edge with weight w i. The input file will not contain duplicate edges. The end-of-file is denoted by a single line containing "0 0".
 

Output
For each input test case, print a single integer indicating the minimum total weight of edges that must be deleted in order to ensure that there exists no path from one of the original leaves to the root.
 

Sample Input
  
  
15 15 1 2 1 2 3 2 2 5 3 5 6 7 4 6 5 6 7 4 5 15 6 15 10 11 10 13 5 13 14 4 12 13 3 9 10 8 8 9 2 9 11 3 0 0
 

Sample Output
  
  
16
 

Source
题意:给出n个结点,根节点r,删除某些边,使得叶子与根节点不联通,求删除这些边的最小权值和。
思路:简单的树形DP,dp[i]表示使得i的叶子不与i联通要删除的权值的最小值,这样dp[fa] += min(dp[son], cost(fa, son)),删除儿子与父亲直接相连的边或者处理与该儿子相连的叶子。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 1005
using namespace std;
int dp[maxn], head[maxn];
struct Edge{
    int to, next, cost;
}edge[maxn*2];
int n, tot;
void add(int u, int v, int cost){
    edge[tot].to = v;
    edge[tot].cost = cost;
    edge[tot].next = head[u];
    head[u] = tot++;
}
void dfs(int fa, int pre){
    if(head[fa]==-1||(edge[head[fa]].to==pre&&edge[head[fa]].next==-1)) return;
    dp[fa] = 0;
    for(int i = head[fa];i != -1;i = edge[i].next){
        int son = edge[i].to;
        if(son == pre) continue;
        dfs(son, fa);
        dp[fa] += min(dp[son], edge[i].cost);
    }
}
int main()
{
    int r, i, j, u, v, cost;
    while(~scanf("%d %d", &n, &r)){
        if(!n&&!r) break;
        tot = 1;
        memset(head, -1, sizeof head);
        memset(dp, 127, sizeof dp);
        for(i = 0;i < n-1;i++){
            scanf("%d %d %d", &u, &v, &cost);
            add(u, v, cost);
            add(v, u, cost);
        }
        dfs(r, 0);
        if(n == 1) dp[r] = 0;
        printf("%d\n", dp[r]);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值