hdu 4313 Matrix

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QingyingLiu/article/details/82024112

Matrix

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3567 Accepted Submission(s): 1419

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

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

分析

这道题目可以采用和kruskal算法类似的思想来求解。考虑到正向思考我们不能分割集合,所以我们采用的思想是反向思考-合并集合。
所以我们采用的贪心策略是:将尽可能大的边放入图中,而剩下的边就是需要分割的最小边。细节就是:先将edge降序排序,我们假设边的顶点为u,v,
如果u,v都有machine,那么我们必须分割这条边:res+=edge.w,否则我们就合并集合(并查集)。

Accepted code

/*************************************************************************
  > File Name: Matrix.cpp
  > Author: Lewin
  > Mail: [email protected]
  > Created Time: 2018年08月24日 星期五 18时07分22秒
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=1000005;

int f[maxn];
int sum[maxn];
struct Edge{
    int u,v,w;
    Edge(){}
    Edge(int uu,int vv,int ww):u(uu),v(vv),w(ww){}
    bool operator <(const Edge& obj)const{
        return w>obj.w;
    }
}edge[maxn];

void init(int n){
    for(int i=0;i<=n;i++)
        f[i]=i;
    memset(sum,0,sizeof(sum));
}

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

void Union(int x,int y){
    int fx=find(x),fy=find(y);
    f[fx]=fy;
    sum[fy]+=sum[fx];
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,k;
        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].w);
        sort(edge,edge+n-1);
        for(int i=0;i<k;i++){
            int loc;
            scanf("%d",&loc);
            sum[loc]=1;
        }

        int cnt=0;
        long long int res=0;
        for(int i=0;i<n-1&&cnt<k-1;i++){
            int u=edge[i].u,v=edge[i].v;
            int fu=find(u),fv=find(v);
            if(sum[fv]&&sum[fu])
                res+=edge[i].w;
            else{
                Union(fu,fv);
            }
        }

        printf("%lld\n",res);
        }
}

猜你喜欢

转载自blog.csdn.net/QingyingLiu/article/details/82024112