HDU 4313树形DP
Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3232 Accepted Submission(s): 1283
unique path between any pair of cities.
Morpheus has the news that K Machines are planning to destroy the whole kingdom. These Machines are initially living in K different cities of the kingdom and
anytime from now they can plan and launch an attack. So he has asked Neo to destroy some of the roads to disrupt the connection among Machines. i.e after destroying those roads there should not be any path between any two Machines.
Since the attack can be at any time from now, Neo has to do this task as fast as possible. Each road in the kingdom takes certain time to get destroyed and they
can be destroyed only one at a time.
You need to write a program that tells Neo the minimum amount of time he will require to disrupt the connection among machines.
For each test case the first line input contains two, space-separated integers, N and K. Cities are numbered 0 to N-1. Then follow N-1 lines, each containing three, space-separated integers, x y z, which means there is a bidirectional road connecting city x and city y, and to destroy this road it takes z units of time.Then follow K lines each containing an integer. The ith integer is the id of city in which ith Machine is currently located.
2 <= N <= 100,000
2 <= K <= N
1 <= time to destroy a road <= 1000,000
Neo can destroy the road connecting city 2 and city 4 of weight 5 , and the road connecting city 0 and city 1 of weight 5. As only one road can be destroyed at a
time, the total minimum time taken is 10 units of time. After destroying these roads none of the Machines can reach other Machine via any path.
题意:
有n个点,n-1条边的树,边有权值,有k个坏点,要求去掉一些边让所有的坏点不相互联通,问去掉的最小边权值。
代码:
//两个相邻的点都是坏点时必须去掉他们之间的边,儿子是坏点父亲不是坏点时
//可以至少保留1个儿子坏点去掉其他的兄弟坏点的边,我们必然会保留边权值大的
//那个儿子,然后向他的祖先传递坏点信息并更新dp(隔离那个坏点的最小边权)。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int t,n,k,dp[],f[],ma[],head[],tol;
ll ans;
struct node{
int v,w,next;
}nodes[];
void Add(int x,int y,int z){
nodes[tol].v=y;
nodes[tol].w=z;
nodes[tol].next=head[x];
head[x]=tol++;
}
void dfs(int u,int fa){
dp[u]=inf;
ma[u]=f[u];
ll tmp=;
for(int i=head[u];i!=-;i=nodes[i].next){
int v=nodes[i].v;
if(v==fa) continue;
dfs(v,u);
if(!ma[v]) continue;
if(f[u]) ans+=min(nodes[i].w,dp[v]);
else{
ans+=min(tmp,1LL*min(dp[v],nodes[i].w));
tmp=max(tmp,1LL*min(dp[v],nodes[i].w));
}
}
if(tmp!=){
dp[u]=tmp;
ma[u]=;
}
}
int main()
{
scanf("%d",&t);
while(t--){
tol=;
memset(head,-,sizeof(head));
scanf("%d%d",&n,&k);
int x,y,z;
for(int i=;i<n;i++){
scanf("%d%d%d",&x,&y,&z);
Add(x,y,z);
Add(y,x,z);
}
memset(f,,sizeof(f));
for(int i=;i<k;i++){
scanf("%d",&x);
f[x]=;
}
memset(ma,,sizeof(ma));
ans=;
dfs(,-);
printf("%I64d\n",ans);
}
return ;
}
HDU 4313树形DP的更多相关文章
- hdu 4123 树形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...
- HDU 1520 树形dp裸题
1.HDU 1520 Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...
- HDU 1561 树形DP入门
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 2196树形DP(2个方向)
HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...
- HDU 1520 树形DP入门
HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...
- codevs 1380/HDU 1520 树形dp
1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 回到问题 题目描述 Description Ural大学有N个职员 ...
- HDU 5834 [树形dp]
/* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...
- hdu 4267 树形DP
思路:先dfs一下,找出1,n间的路径长度和价值,回溯时将该路径长度和价值清零.那么对剩下的图就可以直接树形dp求解了. #include<iostream> #include<al ...
- hdu 4607 (树形DP)
当时比赛的时候我们找出来只要求出树的最长的边的节点数ans,如果要访问点的个数n小于ans距离直接就是n-1 如果大于的话就是(n-ans)*2+ans-1,当时求树的直径难倒我们了,都不会树形dp ...
随机推荐
- CsvHelper文档-1前言
CsvHelper文档-1前言 英文文档链接地址:CsvHelper Document 开源项目地址:CsvHelper 翻译于2018-1-5,原本可能会随时更新: 每一段代码都是经过我实际测试的, ...
- IntelliJ IDEA 2018 for MAC安装及破解
---------------------说在前面-------------------------- IntelliJ IDEA 2018 版本为2018.1.4 教程按照下载安装sdk.破解两部分 ...
- mac上golang编译出现clang错误
错误现象 几周前,突然发现我的go 项目编译开始报一种以前从来没有出现过的错误: # runtime/cgo clang: warning: argument unused during compil ...
- 【转】自定义(滑动条)input[type="range"]样式
1.如何使用滑动条? 用法很简单,如下所示: <input type="range" value="0"> 各浏览器原始样式如下: Chrome: ...
- YaoLingJump开发者日志(九)V1.1.1版本完成
跳跃吧瑶玲下载连接 百度网盘下载 介绍 Android版本升级到8.0,发现原来的图标不显示了: 百度了一下解决方案,用了该博客提供的第二种方法,修改roundIcon的内容. 可爱的图标 ...
- 3ds Max学习日记(四)
下午去实验室见了师姐,人还挺好,给我安排了任务,和3ds max没有半毛钱关系. 附上今日的劳动成果: 板子(牌匾) 简约吊灯(看上去比较单调) 高脚杯(喝酒用的) 沙发(沙发) ...
- jconsole工具监控java运行情况
jconsole是jdk自带的工具.所以要先安装jdk 1.jconsole工具的路径: 通过which jconsole来查看 /usr/local/jdk1.7.0_79/bin/jconsol ...
- Directory类的使用、Alt+Shift+F10可以查看其命名空间
对于一个对象,按下Alt+Shift+F10可以查看其命名空间. Directory类的使用 using System; using System.Collections.Generic; using ...
- shmem:
在/proc/meminfo中发现,cached不等于ActiveFile + InActiveFile,我们来看看cache到底都包括啥内存 1)首先肯定包含activeFile 和 inactiv ...
- Maven pom 文件解释
1 - 什么是构建? 我们都知道,写完代码之后需要进行编译和运行,以笔者自身为例,使用 Eclipse 写完代码,需要进行编译,再生成 war 包,以便部署到 Tomcat. 在编写 Java 代码的 ...