Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3563 Accepted Submission(s): 1418
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
题意: 有n个节点,n-1条边,其中k个节点为危险节点,有大规模杀伤性武器,切断哪些路能使得这些大规模杀伤性武器的危险节点之间彼此不连通,且切断的边权值之和最小。
初始化每个节点为一个集合,并记录每个集合中危险节点的数目(0或1)。
要实现权值之和尽可能的小,则要权值尽可能小,故先将n-1条边按权值先降序排序。
排序后枚举这些边:
若边的两端节点所在集合均有大规模杀伤性武器,则删除它并累计其权值。
若只有一边有,则合并这两个集合(用并查集),合并时尽可能将危险节点置于父节点位置。
若没有,则合并这两个集合。
枚举结束,输出权值累计值即可。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n,k,flag[maxn],father[maxn];
struct edge
{
int u,v,c;
}rd[maxn];
bool cmp(edge a,edge b)
{
return a.c>b.c;
}
int Find(int x)
{
while(x!=father[x]) x=father[x];
return x;
}
void mix(int x,int y)
{
father[x]=y;
}
void solve()
{
sort(rd,rd+n-1,cmp);
long long ans=0;
for(int i=0;i<n-1;i++)
{
int newx=Find(rd[i].u),newy=Find(rd[i].v);
if(flag[newx]&&flag[newy])
ans+=rd[i].c;
else if(flag[newx])
mix(newy,newx);
else mix(newx,newy);
}
printf("%lld\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
memset(flag,0,sizeof(flag));
for(int i=0;i<n;i++)
{
father[i]=i;
if(i==n-1) break;
scanf("%d%d%d",&rd[i].u,&rd[i].v,&rd[i].c);
}
for(int i=0;i<k;i++)
{
int temp;
scanf("%d",&temp);
flag[temp]=1;
}
solve();
}
}