HDU3452 Bonsai(树形DP)

来源:互联网 发布:linux解压缩rar 编辑:程序博客网 时间:2024/05/11 14:44

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 ui vi wi (where ui, vi ∈ {1,……, n} and 0 <= wi <= 1000) indicating that vertex ui is connected to vertex vi by an undirected edge with weight wi. 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 151 2 12 3 22 5 35 6 74 6 56 7 45 15 615 10 1110 13 513 14 412 13 39 10 88 9 29 11 30 0
 

Sample Output
16
 

Source
2009 Stanford Local ACM Programming Contest
题意:给出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 1005using 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 0
原创粉丝点击