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代码:
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; }
相关文章推荐
- 2012 Multi-University Training Contest 2 Matrix
- HDU 4349 Xiao Ming's Hope 2012 Multi-University Training Contest 5
- HDU6033 & 2017 Multi-University Training Contest - Team 1 A
- hdu 4322 Candy(最大费用流)2012 Multi-University Training Contest 3
- HDU 5347 MZL's chemistry (2015 Multi-University Training Contest 5 2015多校联合)
- hdu 4365 Palindrome graph (规律题 + 快速幂取模 2012 Multi-University Training Contest 7 )
- hdu 4366 Successor (线段树 2012 Multi-University Training Contest 7 )
- 2012 Multi-University Training Contest 6-1009 hdu4358 Boring counting
- hdu 4336 Card Collector 2012 Multi-University Training Contest 4
- Moonfang's Birthday&&2011 Multi-University Training Contest 3 - Host by BIT
- 2012 Multi-University Training Contest 1 Divide Chocolate(递推)
- hdu4323-Magic Number-2012 Multi-University Training Contest 3题解
- hdu4325-Flowers-2012 Multi-University Training Contest 3-题解
- HDU6044 & 2017 Multi-University Training Contest - Team 1
- 2012 Multi-University Training Contest 5-1001 hdu4340
- hdu 4340 Capturing a country(树形 dp) (2012 Multi-University Training Contest 5 )
- HDU 5352 MZL's City(最小费用最大流)经典 2015 Multi-University Training Contest 5
- 2017 Multi-University Training Contest 3 && HDOJ 6058 Kanade's sum 【链表模拟】
- hdu 4396 More lumber is required (二维最短路 应用 2012 Multi-University Training Contest 10 )
- 2017 Multi-University Training Contest - Team 5:1006&hdu6090、 Rikka with Graph