您的位置:首页 > 大数据 > 人工智能

Matrix&&2012 Multi-University Training Contest 2

2012-07-28 10:29 513 查看
[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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

[align=left]Output[/align]
For each test case print the minimum time required to disrupt the connection among Machines.

[align=left]Sample Input[/align]

1
5 3
2 1 8
1 0 5
2 4 5
1 3 4
2
4
0

[align=left]Sample Output[/align]

10
题意:给你n个点n-1条边,其中有k个顶点有机器人,为使得每个机器人都不连通,问你所需删除的最小的权值和是多少?思路:krusal的变形,按边权从大到小排序,如果两个城市不都有机器人则合并,并更新每个城市的机器人,否者删除。AC代码:
#include<cstdio>
#include<string.h>
#include<algorithm>
#define N 100005
using namespace std;
typedef struct node
{
int x;
int y;
int len;
bool operator<(node a)const
{return len>a.len;}
}Node;
Node s
;
int Father
;
bool flag
;
int n,m;
void in(int &a)
{
char ch;
while((ch=getchar())<'0'||ch>'9');
for(a=0;ch>='0'&&ch<='9';ch=getchar()) a=a*10+ch-'0';
}
int Find(int x)
{
if(x==Father[x]) return x;
return Father[x]=Find(Father[x]);
}
void init()
{
in(n),in(m);
for(int i=0;i<n;++i) Father[i]=i,flag[i]=false;
for(int i=0;i<n-1;++i)in(s[i].x),in(s[i].y),in(s[i].len);
sort(s,s+n-1);
for(int i=0;i!=m;++i)
{
int a;
in(a);
flag[a]=true;
}
}
int main()
{
int T;
in(T);
while(T--)
{
init();
__int64 sum=0;
for(int i=0;i!=n-1;++i)
{
int x=Find(s[i].x);
int y=Find(s[i].y);
if(flag[x]&&flag[y]){sum+=s[i].len;continue;}
if(flag[x]||flag[y]) flag[x]=flag[y]=true;
Father[x]=y;
}printf("%I64d\n",sum);
}return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐