您的位置:首页 > 其它

hdu 4313 Matrix

2014-09-24 12:29 267 查看
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(2<=n<=100000)个节点的树,树中每条边都有一个权值。然后再给你k(2<=k<=n)个点,表示这些点上有一个机器人。最后让你删去一些边使任意两个机器人都不能互达,且所删边的权值之和要最小。

对边进行排序,先选大的,如果对加入的边不能使得有两个机器人可以通路,则加入这条边,否则就不加,最后剩下的就是就是要摧毁的边了。

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;
int Father[100005];
int sum[100005];
int n;
struct node
{
int a,b;
int w;
bool operator<(const node &t)const
{
return t.w<w;
}
};
int Find(int x)
{
if(x!=Father[x])
{
Father[x]=Find(Father[x]);
}

return Father[x];
}
int main()
{
int t,n,k;
node num[100005];
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&k);
long long ans=0;
for (int i=0; i<=n; i++)
{
Father[i]=i;
sum[i]=0;
}
for (int i=0; i<n-1; i++)
{
scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].w);
ans+=num[i].w;
}
for (int i=0; i<k; i++)
{
int a;
scanf("%d",&a);
sum[a]=1;    //该城市有机器人
}
sort(num, num+n-1);
for (int i=0; i<n-1; i++)
{
int x=Find(num[i].a);
int y=Find(num[i].b);
if(sum[x]+sum[y]<=1)   //两个城市之间最多有一个城市有机器人
{
if(x!=y)
{
Father[x]=y;
sum[y]+=sum[x];
}
ans-=num[i].w;
}
}
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: