hdu 4313 Matrix(最大生成树)
2013-08-02 00:07
555 查看
Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1835 Accepted Submission(s): 703
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.
题意:给出n个点,n-1条边,给出k个点,要使这k个点互不相连,要求删除最小权值的边,输出删除的边的权值总和。
思路:生成k课最大生成树,删除k-1条边。用kruskal算法。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <stack> #include <cstdlib> using namespace std; const int maxn=100005; struct node { int u,v,w; } edge[maxn]; int n,k; int fa[maxn]; bool loc[maxn]; bool cmp(node a,node b) { return a.w>b.w; } void init() { for(int i=0; i<n; i++) fa[i]=i; memset(loc,false,sizeof(loc)); } int Find_set(int x) { if(x==fa[x]) return fa[x]; fa[x]=Find_set(fa[x]); return fa[x]; } void Union(int ra,int rb) { if(loc[ra]) fa[rb]=ra; else fa[ra]=rb; } int main() { int t,a; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&k); init(); for(int i=0; i<n-1; i++) scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w); while(k--) { scanf("%d",&a); loc[a]=true; } sort(edge,edge+n-1,cmp); long long ans=0; for(int i=0; i<n-1; i++) { int x=Find_set(edge[i].u); int y=Find_set(edge[i].v); if(loc[x]&&loc[y]) ans+=edge[i].w; else Union(x,y); } printf("%I64d\n",ans); } return 0; }
相关文章推荐
- HDU 2830 Matrix Swapping II (最大子矩阵)
- HDU 4786 Fibonacci Tree 最小生成树 + 最大生成树 (2013成都区域赛 F题)
- 【HDU 6187 】Destroy Walls 【平面图+最大生成树】
- (hdu step 6.1.8)Pseudoforest(求有一个环的最大生成树)
- HDU 4313 Matrix
- HDU 2686 Matrix(最大费用流)
- HDU-4313 Matrix 最小生成树,集合划分
- hdu 4313 Matrix 树形dp
- hdu 4313 Matrix 并查集 多校联合赛(二) 第四题
- hdu 3367 Pseudoforest(最大生成树)
- Largest Submatrix-最大子矩阵-HDU-2870
- hdu 4313 Matrix (最小生成树krusual)
- hdu 1797 靠谱的算法应该是最大生成树,但是本人用最大流做的
- hdu 3367 Pseudoforest 最大生成树
- hdu 4313 Matrix
- HDU 2870 Largest Submatrix (最大子矩阵)
- HDU 4313 Matrix 类似 克鲁斯卡尔算法
- hdu 2870 Largest Submatrix(最大子矩阵变式)
- HDU 4313 Matrix
- Matrix (hdu 2686 最大费用最大流)