您的位置:首页 > 其它

HDU 4313 Matrix (最小生成树)

2016-05-11 16:36 453 查看
Matrix

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

Total Submission(s): 3077 Accepted Submission(s): 1194

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

题意:给出N个区域0--N-1,N-1个连接x y z(x y 这两个区域连接的时间)

现在给出m个危险区域,求将m个危险区域分开的最小的时间(将n-1个关系的时间从大到小排序)(然后依次建立关系)

之所以从大到小来排序是因为当两者之间均为危险区域时的时间已经确定,

当一个是危险区域(或者均不是危险区域)时则建立关系时让权从大到小排序,最终才会取得少的时间

只需让m个区域做根结点,连接这m个区域的权的和便是利用的最少的时间

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;
const int maxx=100005;///N<100000;
int par[maxx];
bool flag[maxx];///存放m个危险区域
struct rode
{
int u;
int v;
long long int w;
} s[maxx];
int n,m;
int cmp(rode a,rode b)
{
return a.w>b.w;///按照时间从大到小排序

}
void init()
{
for(int i=0; i<=n; i++)///初始化时要从0开始!!!WA几次啦 0————N-1
par[i]=i;
}

int find(int x)
{
if(par[x]==x)
return x;
else
return par[x]=find(par[x]);
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0; i<n-1; i++)
{
scanf("%d%d%lld",&s[i].u,&s[i].v,&s[i].w);
}
sort(s,s+n-1,cmp);
int x;
memset(flag,false,sizeof(flag));
while(m--)
{
scanf("%d",&x);
flag[x]=true;
}

int a,b;
init();
long long int sum=0;
for(int i=0; i<n-1; i++)
{
a=find(s[i].u);
b=find(s[i].v);
if(flag[a]&&flag[b])///如果两个根结点均为危险区域,则连接他们的权和即为最小和
{
sum+=s[i].w;
}
else if(flag[a])
par[b]=a;
else
par[a]=b;
}
printf("%I64d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: