您的位置:首页 > 其它

hdu 4313 Matrix

2012-07-27 11:10 363 查看



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.


贪心,DP都能做,DP思路清晰一些,可是树形DP不会搞啊。。。。尴尬

比赛的时候有想到用kruskal的思想,但是卡在如何判连通问题上了,加上1002没出,思维两边走,双开的结果就是都没做出来。。。。

其实判连通只要重开一个数组,然后记录危险点,如果有点和危险点在一个集合,那么该点的根节点也为危险点

从大到小开始排序,从大边开始放入集合,不能入集合的即为删除边

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
const int maxn=100005;
struct in
{
int x,y;
long long w;
}a[maxn];
int n;
bool cmp(in p,in q)
{
return p.w>q.w;
}
bool sp[maxn];
int belg[maxn];
void init()
{
for(int i=0;i<n;i++)
belg[i]=i;
}
int  findx(int x)
{
return x==belg[x]?x:findx(belg[x]);
}
void Union(int a,int b)
{
int ra=findx(a),rb=findx(b);
belg[ra]=rb;
if(sp[ra]==1)
sp[rb]=1;
}
int main()
{
int t,xx;
scanf("%d",&t);
while(t--)
{
memset(sp,0,sizeof(sp));
int k;
long long sum=0;
scanf("%d%d",&n,&k);
for(int i=0;i<n-1;i++)
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
for(int i=0;i<k;i++)
{
scanf("%d",&xx);
sp[xx]=1;
}
sort(a,a+n-1,cmp);
init();
for(int i=0;i<n-1;i++)
{
int rx=findx(a[i].x);
int ry=findx(a[i].y);
if(sp[rx] && sp[ry])
sum+=a[i].w;
else
Union(rx,ry);
}
cout<<sum<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: