HDU4313Matrix(用最小生成树思想,集合划分)
来源:互联网 发布:grub修复系统引导linux 编辑:程序博客网 时间:2024/06/15 15:23
Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2598 Accepted Submission(s): 973
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.
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
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
15 32 1 81 0 52 4 51 3 4240
Sample Output
10HintNeo 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 atime, 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.
解析:要求给出的k个点分开,因为任意两个集合点只有一条相连,所以我们把这k个点当成k个集合点的根节点,那么连接这k个集合的边,它们的权值加起来就是我们要的最小和了。我们将边按从大到小的顺序排好序,然后就是看是否这条边能够使得两个同类的节点连在一起,如果能够的话,那么这条边就是我们要选择的划分边。
#include<stdio.h>#include<algorithm>using namespace std;#define LL __int64const int N = 100005;struct EDG{ int u,v; LL c;};int fath[N],typeNode[N],n;EDG edg[N];void init(){ for(int i=0;i<=n;i++) { fath[i]=i; typeNode[i]=0; }}int findfath(int x){ if(x!=fath[x]) fath[x]=findfath(fath[x]); return fath[x];}LL setfath(int i){ int a=findfath(edg[i].u); int b=findfath(edg[i].v); if(a==b) return 0; if(typeNode[a]&&typeNode[b]) return edg[i].c; if(typeNode[a]) fath[b]=a; else fath[a]=b; return 0;}int cmp(EDG a,EDG b){ return a.c>b.c;}int main(){ int t,k,a; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&k); init(); for(int i=0;i<n-1;i++) scanf("%d%d%I64d",&edg[i].u,&edg[i].v,&edg[i].c); sort(edg,edg+n-1,cmp); while(k--) { scanf("%d",&a); typeNode[a]=1; } LL ans=0; for(int i=0;i<n-1;i++) ans+=setfath(i); printf("%I64d\n",ans); }}
0 0
- HDU4313Matrix(用最小生成树思想,集合划分)
- HDU4313-2012多校二-最小生成树,集合划分
- 最小生成树-(贪心思想)
- 最小生成树 贪心算法思想
- 最小生成树MST的Kruskal算法+并查集(链表实现)划分连通分量和集合,并查集可以保存多个集合
- <考试题> bzoj 1821 部落划分 (二分、最小生成树)
- bzoj1821: [JSOI2010]Group 部落划分 Group(最小生成树)
- 【UVa】11354 Bond 最小生成树,动态LCA,倍增思想
- POJ 2349 Arctic Network(最小生成树思想)
- 最小生成树,并查集的思想 nyoj1239
- 最小生成树的Prime算法的思想
- 集合划分的词典序生成
- 最小生成树kruskal 并查集合 模板题
- 【并查集合并注意!!!!】【最小生成树】
- <二分答案加验证||最小生成树>bzoj 1821 部落划分
- 次小生成树的思想 pku1679最小生成树是否唯一
- 证明两个集合的划分最小绝对值差问题
- POJ3469根据划分两集合的最小割
- pcb封装1
- 批处理文件夹里的图片
- 索引图像处理的一些心得
- 简单链表的实现
- Java中的大小写字母相互转换(不利用Java自带的方法)
- HDU4313Matrix(用最小生成树思想,集合划分)
- LeetCode - Jump Game II
- JAVA基础知识
- ISAP算法
- ecmall模板编辑中的标题如何自定义读取
- RFID理论基础知识要点
- 不用算术运算符实现两个数的加法操作
- 使用jquery-validationEngine框架,4步实现前端JS校验
- HDU 1285 确定比赛名次(拓扑排序)