Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3538 Accepted Submission(s): 1405
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.
Author
TJU
Source
2012 Multi-University Training Contest 2
问题链接:HDU4313Matrix
问题分析:题目大意为给定n个点n-1条边,指定其中的k个点,问如何删除其中的边使得k个点互不连通,且删除的边的总长度最短。具体思路看代码,如果不能理解的话,可以尝试手工计算测试样例。
程序说明:使用Kruskal算法和并查集
AC的C++程序:
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
set<int>s;
const int N=100005;
int pre[N];
void init(int n)
{
for(int i=0;i<n;i++)
pre[i]=i;
}
int find(int x)
{
int r=x;
while(r!=pre[r])
r=pre[r];
while(x!=pre[x]){
int i=pre[x];
pre[x]=r;
x=i;
}
return r;
}
bool join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy){
if(fx>fy){
int temp=fx;
fx=fy;
fy=temp;
}
pre[fx]=fy;
return true;
}
return false;
}
struct Edge{
int u,v,cost;
}edge[N];
bool cmp(Edge &a,Edge &b)
{
return a.cost>b.cost;
}
int main()
{
int t,n,k;
scanf("%d",&t);
while(t--){
s.clear();
scanf("%d%d",&n,&k);
init(n);
for(int i=0;i<n-1;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].cost);
sort(edge,edge+n-1,cmp);
for(int i=1;i<=k;i++){
int x;
scanf("%d",&x);
s.insert(x);
}
long long ans=0;
int cnt=0;
for(int i=0;i<n-1;i++){
int u=find(edge[i].u);
int v=find(edge[i].v);
if(s.count(u)!=0&&s.count(v)!=0){
ans+=edge[i].cost;
cnt++;
if(cnt==k-1)
break;
}
else{
if(s.count(u)!=0)
pre[v]=u;
else
pre[u]=v;
}
}
printf("%lld\n",ans);
}
return 0;
}