HDU 3452 Bonsai(树形dp)
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
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
Output
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
#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)的更多相关文章
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- hdu 6201 【树形dp||SPFA最长路】
http://acm.hdu.edu.cn/showproblem.php?pid=6201 n个城市都在卖一种书,该书的价格在i城市为cost[i],商人打算从某个城市出发到另一个城市结束,途中可以 ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...
- hdu 4081 最小生成树+树形dp
思路:直接先求一下最小生成树,然后用树形dp来求最优值.也就是两遍dfs. #include<iostream> #include<algorithm> #include< ...
- HDU 3452 Bonsai(网络流之最小割)
题目地址:HDU 3452 最小割水题. 源点为根节点.再另设一汇点,汇点与叶子连边. 对叶子结点的推断是看度数是否为1. 代码例如以下: #include <iostream> #inc ...
- HDU 3899 简单树形DP
题意:一棵树,给出每个点的权值和每条边的长度, 点j到点i的代价为点j的权值乘以连接i和j的边的长度.求点x使得所有点到点x的代价最小,输出 虽然还是不太懂树形DP是什么意思,先把代码贴出来把. 这道 ...
- HDU 4714 Tree2cycle (树形DP)
题意:给定一棵树,断开一条边或者接上一条边都要花费 1,问你花费最少把这棵树就成一个环. 析:树形DP,想一想,要想把一棵树变成一个环,那么就要把一些枝枝叶叶都换掉,对于一个分叉是大于等于2的我们一定 ...
- hdu Anniversary party 树形DP,点带有值。求MAX
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 4313 Matrix 树形dp
题意: 给定n个点的树,m个黑点 以下n-1行给出边和删除这条边的费用 以下m个黑点的点标[0,n-1] 删除一些边使得随意2个黑点都不连通. 问删除的最小花费. 思路: 树形dp 每一个点有2个状态 ...
随机推荐
- 用户登录失败,该用户与可信SQL Server连接无关联,错误:18452
安装好SQLServer2005(或者装了Visual Studio 2008后自带的SQLServer2005)用SQL Server身份验证的登录的时候有时候会发生这种情况: 这样的错误的原因是: ...
- SVN入门-2分钟教你入门
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010540106/article/details/37317201 学习SVN首先我们应该知道 ...
- rtsp over udp
#include <stdio.h> #include <netinet/in.h> #include <sys/socket.h> #include <st ...
- Xilinx中解决高扇出的方法
Fanout,即扇出,指模块直接调用的下级模块的个数,如果这个数值过大的话,在FPGA直接表现为net delay较大,不利于时序收敛.因此,在写代码时应尽量避免高扇出的情况.但是,在某些特殊情况下, ...
- linux 或c 时间相关处理类型和函数
注意1.精确级别,纳秒级别原型long clock_gettime (clockid_t which_clock, struct timespec *tp); 头文件time.hwhich_cloc ...
- git 上传本地代码到远程仓库
未经允许,禁止转载! 查看哪些文件被修改过:git status 查看具体的修改内容:git diff 对新添加的文件进行添加:git add 文件名 提交修改标记:git commit -m &qu ...
- PAT 天梯赛 L1-007. 念数字 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-007 AC代码 #include <iostream> #include <cstdio&g ...
- UVALive 6915 J - Leveling Ground
思路: 简单模拟下.从左向右扫描一次,求出挖出该区间空地的花费,并取个最小值即可. 至于怎么求区间内的高度最小值,就用线段树就好了. #include <bits/stdc++.h> #d ...
- Unity,如何阻塞当前函数一段时间
public class Example : MonoBehaviour { IEnumerator Example() { print(Time.time); ); print(Time.time) ...
- 整合subeclipse和Tortoise SVN
先来一个下载链接(subeclipse1.8和TortoiseSVN1.7): http://download.csdn.net/detail/cangfengluyu/8416395 对于同 ...