標籤:
Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2611 Accepted Submission(s): 978
Problem DescriptionMachines 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.
InputThe 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
OutputFor each test case print the minimum time required to disrupt the connection among Machines.
Sample Input15 32 1 81 0 52 4 51 3 4240
Sample Output10HintNeo 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.
AuthorTJU 將邊權從大到小排序,然後如果某條邊兩遍最多隻有一個機器人,直接減掉。
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<cstdlib>#include<algorithm>using namespace std;int n,k,fa[100010],mark[100010];struct node{ int x,y,z;}e[100010];bool cmp(node a,node b){ return a.z>b.z;}int find(int x){ if(x!=fa[x]) fa[x]=find(fa[x]); return fa[x];}int main(){ int tt,x; scanf("%d",&tt); while(tt--) { long long ans=0; scanf("%d%d",&n,&k); memset(mark,0,sizeof(mark)); for(int i=0;i<n;i++) fa[i]=i; for(int i=0;i<n-1;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z),ans+=e[i].z; sort(e,e+n-1,cmp); while(k--) { scanf("%d",&x); mark[x]=1; } for(int i=0;i<n-1;i++) { int fx,fy; fx=find(e[i].x); fy=find(e[i].y); if(mark[fx]+mark[fy]<=1) { if(fx!=fy) { fa[fx]=fy; mark[fy]+=mark[fx]; } ans-=e[i].z; } } printf("%I64d\n",ans); } return 0;}
(最大產生樹) hdu 4313