zoukankan      html  css  js  c++  java
  • MUTC 2 D Matrix 并查集

    Matrix

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


    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
     

    Recommend
    zhuyuanchen520
     

    -----------------

    按边权从大到小合并,若有矛盾则累计矛盾边的权值。

    -----------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn=111111;
    struct edgenode{
        int u,v,w;
    }edges[maxn];
    bool boom[maxn];
    bool cmp(edgenode a,edgenode b)
    {
        return a.w>b.w;
    }
    
    int p[maxn];
    void make_set(int n)
    {
        for (int i=0;i<=n;i++) p[i]=i;
    }
    int find_set(int x)
    {
        if (x!=p[x]) p[x]=find_set(p[x]);
        return p[x];
    }
    void unin(int x,int y)
    {
        bool ok=false;
        if (boom[x]||boom[y])
        {
            ok=true;
        }
        x=find_set(x);
        y=find_set(y);
        if (x!=y) p[x]=y;
        if (ok) boom[y]=true;
    }
    int n,k;
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while (T--)
        {
            memset(boom,0,sizeof(boom));
            scanf("%d%d",&n,&k);
            make_set(n);
            for (int i=1;i<n;i++)
            {
                scanf("%d%d%d",&edges[i].u,&edges[i].v,&edges[i].w);
            }
            for (int i=1;i<=k;i++)
            {
                int t;
                scanf("%d",&t);
                boom[t]=true;
            }
            sort(edges+1,edges+n,cmp);
            long long ans=0;
            for (int i=1;i<n;i++)
            {
                int u=find_set(edges[i].u);
                int v=find_set(edges[i].v);
                int w=edges[i].w;
                if (boom[u]&&boom[v])
                {
                    ans+=w;
                }
                else
                {
                    unin(u,v);
                }
            }
            printf("%I64d\n",ans);
        }
        return 0;
    }
    




  • 相关阅读:
    BAT带队烧钱圈地华为们猛追云计算
    各浏览器的cookie的name个数/最大容量限制测试
    多备份:云端数据物流平台为企业提供云备份服务(通过增值服务盈利,数据备份相当于买保险)
    多备份CEO胡茂华:创业路上的五道坎
    蜡笔同步:同步通讯录,同步短信,用电脑发短信
    MIUI是小米的核心竞争力
    tggg
    Ubuntu 创建启动器
    TProcedure,TMethod,TNotifyEvent,TWndMethod的区别,并模拟点击按钮后发生的动作
    所有语言的Awesome
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226367.html
Copyright © 2011-2022 走看看