您的位置:首页 > 其它

hdu 4313 Matrix(并查集)

2013-08-02 00:22 375 查看


Matrix

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1844 Accepted Submission(s): 706



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

2012 Multi-University Training Contest 2

Recommend

zhuyuanchen520

题意:给定n个点,n-1条边,其中有k个危险点,删去最少总权值的边使这些危险点不连通,问该值

题解:先将边按权值排序,由大到小,尽量把大权值的边取了,用并查集计算是否相连,危险点初始父亲为-1则可以区分

#include<stdio.h>
#include<stdlib.h>
struct edge{
int x,y,len;
}v[100005];
int cou,fath[100005],n,k;
long long res;
int cmp(const void *a,const void *b)
{
struct edge c=*(struct edge *)a;
struct edge d=*(struct edge *)b;
return c.len<d.len?1:-1;
}
void add(int x,int y,int len)
{
v[cou].x=x;
v[cou].y=y;
v[cou++].len=len;
}
int fin(int x)
{
if(x==fath[x]||fath[x]==-1) return fath[x];
else return fath[x]=fin(fath[x]);
}
int main()
{
int i,t,x,y,z;

//freopen("t.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(cou=i=0;i<n-1;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
for(i=0;i<n;i++) fath[i]=i;
for(i=0;i<k;i++)
{
scanf("%d",&x);
fath[x]=-1;
}
qsort(v,n-1,sizeof(v[0]),cmp);
for(res=i=0;i<n-1;i++)
{
x=fin(v[i].x);  y=fin(v[i].y);
if(x==-1&&y==-1) res+=v[i].len;
else if(x==-1) fath[y]=x;
else fath[x]=y;
}
printf("%I64d\n",res);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: