Matrix

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

Problem Description
Machines have once again attacked the kingdom of Xions. The kingdom of Xions has N cities and N-1 bidirectional roads. The road network is such that there is a
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.

 
Input
The first line is an integer T represents there are T test cases. (0<T <=10)
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
 
Output
For each test case print the minimum time required to disrupt the connection among Machines.
 
Sample Input
1
5 3
2 1 8
1 0 5
2 4 5
1 3 4
2
4
0
 
Sample Output
10

Hint

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.

 
Author
TJU
 
Source
 

题意:

有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的更多相关文章

  1. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  2. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

  3. HDU 1561 树形DP入门

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. HDU 2196树形DP(2个方向)

    HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...

  5. HDU 1520 树形DP入门

    HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...

  6. codevs 1380/HDU 1520 树形dp

    1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 回到问题 题目描述 Description Ural大学有N个职员 ...

  7. HDU 5834 [树形dp]

    /* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...

  8. hdu 4267 树形DP

    思路:先dfs一下,找出1,n间的路径长度和价值,回溯时将该路径长度和价值清零.那么对剩下的图就可以直接树形dp求解了. #include<iostream> #include<al ...

  9. hdu 4607 (树形DP)

    当时比赛的时候我们找出来只要求出树的最长的边的节点数ans,如果要访问点的个数n小于ans距离直接就是n-1 如果大于的话就是(n-ans)*2+ans-1,当时求树的直径难倒我们了,都不会树形dp ...

随机推荐

  1. su和sudo的使用

    用于用户身份切换 一.su 命令形式 代表内容 su 切换为root,以non-login shell的方式 su - 切换为root,以login shell的方式 su -l 账号 切换为“账号” ...

  2. “小葵日记”API接口文档

    "小葵日记"项目API接口文档 时间:2017/10/31 (1)用户登录[待完成] POST:127.0.0.1/index/user/login data 数据别称 数据名 数 ...

  3. ACM 第八天

    数据结构和算法目录表 数据结构和算法目录表   C C++ Java 线性结构 1. 数组.单链表和双链表 2. Linux内核中双向链表的经典实现  数组.单链表和双链表  数组.单链表和双链表   ...

  4. 结对作业二——WordCount进阶版

    软工作业三 要求地址 作业要求地址 结对码云项目地址 结对伙伴:秦玉 博客地址 PSP表格 PSP2.1 个人开发流程 预估耗费时间(分钟) 实际耗费时间(分钟) Planning 计划 10 7 · ...

  5. 认识简单的C

  6. C# 知识回顾 - 你真的懂异常(Exception)吗?

    你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...

  7. opencv图像像素值读取

    说到图像像素,肯定要先认识一下图像中的坐标系长什么样. 1. 坐标体系中的零点坐标为图片的左上角,X轴为图像矩形的上面那条水平线:Y轴为图像矩形左边的那条垂直线.该坐标体系在诸如结构体Mat,Rect ...

  8. vc6.0批量加注释

    MATLAB批量加注释的方法非常简单明了,加注释是ctrl+R,去注释是ctrl+T 然后在VC中我对一条一条加注释的方法非常烦恼,我想也许会有简单的方法可以批量家注释.果然,先贴代码 '------ ...

  9. 【Python】python学习之总结

    迭代器: def gen(): a = yield a a = a * yield a for i in gen(): print(i) 创建一个函数,循环体,yield循环到此就返回一个值.调用函数 ...

  10. 第二章 IoC

    什么是IoC 如何配置IOC Bean的生命周期 多环境配置 条件化配置Bean 什么是IOC? IOC有两层含义, 1.控制反转:将对象实例的创建与销毁的权限交给Spring容器管理,而不再是调用对 ...