hdu 4313 Matrix

4 篇文章 0 订阅
4 篇文章 0 订阅

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: 2596736318@qq.com
  > 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);
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值