Bonsai

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 33   Accepted Submission(s) : 13

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

2009 Stanford Local ACM Programming Contest
 
题意:
形成一颗以r为根的树,让所有的叶子节点和r节点没有联系,切割一些边,使切割的边的值之和最小。
题解:
dp[i]表示以i节点为根节点,让叶子节点和i节点分开的最小代价。
#include <iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
struct node
{
int x,w;
node(int a,int b){x=a; w=b;}
};
int n,r;
vector<node> mp[];
int dp[];
void dfs(int k,int fa,int w)
{
dp[k]=w;
int sum;
if (mp[k].size()== && k!=r) sum=;//如果已经到叶子节点了,那么要赋值最大,后面求出来的值在比较中才能有效。
else sum=;
for(int i=;i<mp[k].size();i++)
{
if (mp[k][i].x==fa) continue;
dfs(mp[k][i].x,k,mp[k][i].w);
sum+=dp[mp[k][i].x];
}
//printf("%d: %d\n",k,sum);
dp[k]=min(dp[k],sum);
}
int main()
{
while(~scanf("%d%d",&n,&r))
{
if (n== && r==) break;
for(int i=;i<=n;i++) mp[i].clear();
for(int i=;i<n;i++)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
mp[x].push_back(node(y,w));
mp[y].push_back(node(x,w));
}
memset(dp,,sizeof(dp));
dfs(r,-,);
printf("%d\n",dp[r]);
} return ;
}

HDU 3452 Bonsai(树形dp)的更多相关文章

  1. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. HDU 2196 Computer 树形DP经典题

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...

  3. hdu 6201 【树形dp||SPFA最长路】

    http://acm.hdu.edu.cn/showproblem.php?pid=6201 n个城市都在卖一种书,该书的价格在i城市为cost[i],商人打算从某个城市出发到另一个城市结束,途中可以 ...

  4. HDU 2196 Computer 树形DP 经典题

    给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...

  5. hdu 4081 最小生成树+树形dp

    思路:直接先求一下最小生成树,然后用树形dp来求最优值.也就是两遍dfs. #include<iostream> #include<algorithm> #include< ...

  6. HDU 3452 Bonsai(网络流之最小割)

    题目地址:HDU 3452 最小割水题. 源点为根节点.再另设一汇点,汇点与叶子连边. 对叶子结点的推断是看度数是否为1. 代码例如以下: #include <iostream> #inc ...

  7. HDU 3899 简单树形DP

    题意:一棵树,给出每个点的权值和每条边的长度, 点j到点i的代价为点j的权值乘以连接i和j的边的长度.求点x使得所有点到点x的代价最小,输出 虽然还是不太懂树形DP是什么意思,先把代码贴出来把. 这道 ...

  8. HDU 4714 Tree2cycle (树形DP)

    题意:给定一棵树,断开一条边或者接上一条边都要花费 1,问你花费最少把这棵树就成一个环. 析:树形DP,想一想,要想把一棵树变成一个环,那么就要把一些枝枝叶叶都换掉,对于一个分叉是大于等于2的我们一定 ...

  9. hdu Anniversary party 树形DP,点带有值。求MAX

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. HDU 4313 Matrix 树形dp

    题意: 给定n个点的树,m个黑点 以下n-1行给出边和删除这条边的费用 以下m个黑点的点标[0,n-1] 删除一些边使得随意2个黑点都不连通. 问删除的最小花费. 思路: 树形dp 每一个点有2个状态 ...

随机推荐

  1. linux系统下nginx安装目录和nginx.conf配置文件目录

    linux系统下nginx安装目录和nginx.conf配置文件目录 1.查看nginx安装目录 输入命令 # ps  -ef | grep nginx 返回结果包含安装目录 root      26 ...

  2. Excel的单元格设置下拉选项并填充颜色

    如何在Excel的单元格中加入下拉选项   方法/步骤     第一步:打开excel文档,选中需加入下拉选项的单元格.      第二步:点击菜单中的“数据”->“数据有效性”->“数据 ...

  3. python中的闭包是什么

    当一个嵌套函数在其外部区域引用了一个值时,该嵌套函数就是一个闭包,其意义就是会记录这个值 def A(x): def B(): print(x) return B A(7)() #7

  4. Java集合(6):TreeSet

    一.TreeSet介绍 与HashSet是基于HashMap实现一样,TreeSet是基于TreeMap实现的.TreeSet是一个有序集合,TreeSet中的元素将按照升序排列,缺省是按照自然排序进 ...

  5. java实现简单邮件的发送以及常见问题

    java实现简单邮件的发送以及常见问题 最近遇到个需求需要实现发送邮件的功能,以前做发送邮件功能都是有邮箱用户名密码,通过用户名密码连接对应的SMTP服务器来实现邮件的发送.但是这次用公司内部的邮箱, ...

  6. Dispose 与 close 方法 的区别

    Dispose : 释放 托管 与 非托管资源. Finalize : 释放 非托管资源. Close: 关闭资源后,可以再次使用资源.

  7. ajax与一般处理程序 HTTP协议交互

    1,一般处理程序中 context.Response.ContentType = "text/plain", 则  ajax参数中 也是 text 类型. 2,一般处理程序中 转化 ...

  8. MapReduce:汇总学生表和成绩表为----学生成绩表

    已知两张数据表,其中表一存储的是学生编号.学生姓名:表二存储的是学生编号.考试科目.考试成绩:编写mapreduce程序,汇总两张表数据为一张统一表格. 表一: A001 zhangsan A002 ...

  9. springboot--配置文件加载顺序

    -file:./config(内部配置) -file:./ (内部配置) -classpath:/config (外部配置) -classpath:/ (外部配置) 运维: spring -jar s ...

  10. python标准库学习-SimpleHTTPServer

    这是一个专题 记录学习python标准库的笔记及心得 简单http服务 SimpleHTTPServer 使用 python -m SimpleHTTPServer 默认启动8000端口 源码: &q ...