zoukankan      html  css  js  c++  java
  • Codeforces Gym 100803F There is No Alternative 暴力Kruskal

    There is No Alternative

    题目连接:

    http://codeforces.com/gym/100803/attachments

    Description

    ICPC (Isles of Coral Park City) consist of several beautiful islands.
    The citizens requested construction of bridges between islands to resolve inconveniences of using
    boats between islands, and they demand that all the islands should be reachable from any other
    islands via one or more bridges.

    The city mayor selected a number of pairs of islands, and ordered a building company to estimate
    the costs to build bridges between the pairs. With this estimate, the mayor has to decide the
    set of bridges to build, minimizing the total construction cost.

    However, it is difficult for him to select the most cost-efficient set of bridges among those
    connecting all the islands. For example, three sets of bridges connect all the islands for the
    Sample Input 1. The bridges in each set are expressed by bold edges in Figure F.1.

    Figure F.1. Three sets of bridges connecting all the islands for Sample Input 1
    As the first step, he decided to build only those bridges which are contained in all the sets
    of bridges to connect all the islands and minimize the cost. We refer to such bridges as no
    alternative bridges. In Figure F.2, no alternative bridges are drawn as thick edges for the
    Sample Input 1, 2 and 3.

    Write a program that advises the mayor which bridges are no alternative bridges for the given
    input.

    Input

    The first line contains two positive integers N and M. N represents the number of islands and
    each island is identified by an integer 1 through N. M represents the number of the pairs of
    islands between which a bridge may be built.

    Each line of the next M lines contains three integers Si
    , Di and Ci (1 ≤ i ≤ M) which represent
    that it will cost Ci to build the bridge between islands Si and Di
    . You may assume 3 ≤ N ≤ 500,

    N − 1 ≤ M ≤ min(50000, N(N − 1)/2), 1 ≤ Si < Di ≤ N, and 1 ≤ Ci ≤ 10000. No two bridges
    connect the same pair of two islands, that is, if i 6= j and Si = Sj , then Di 6= Dj . If all the
    candidate bridges are built, all the islands are reachable from any other islands via one or more
    bridges.

    Output

    Output two integers, which mean the number of no alternative bridges and the sum of their
    construction cost, separated by a space.

    Sample Input

    4 4

    1 2 3

    1 3 3

    2 3 3

    2 4 3

    Sample Output

    1 3

    Hint

    题意

    给你一个图,然后问你有哪些边一定在所有的最小生成树上面

    题解:

    点只有500,所以我们直接O(nm)暴力就好了

    我们只用ban掉一开始在最小生成树上的边,然后判断就好了

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    const int maxn = 100050;
    
    int u[maxn],v[maxn],w[maxn],r[maxn];
    int pa[maxn],n,m;
    int vis[maxn];
    int ans1=0,ans2=0;
    bool cmp(int i,int j)
    {
        return w[i]<w[j];
    }
    int fi(int x)
    {
        return pa[x]==x?x:pa[x]=fi(pa[x]);
    }
    int kruskal(int c)
    {
        int ans=0;
        int num=0;
        for(int i=0;i<=n;i++)
            pa[i]=i;
        for(int i=0;i<m;i++)
        {
            if(i==c)continue;
            int e=r[i];
            int x=fi(u[e]);
            int y=fi(v[e]);
            if(x!=y)
            {
                num++;
                vis[i]=1;
                ans+=w[e],pa[x]=y;
            }
            if(num==n-1)break;
        }
        if(num!=n-1)return -1;
        return ans;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            u[i]=x,v[i]=y,w[i]=z;
        }
        for(int i=0;i<m;i++)
            r[i]=i;
        sort(r,r+m,cmp);
        int temp = kruskal(m);
        for(int i=0;i<m;i++)
        {
            if(!vis[i])continue;
            if(temp!=kruskal(i))
            {
                ans1++;
                ans2+=w[r[i]];
            }
        }
        cout<<ans1<<" "<<ans2<<endl;
    }
  • 相关阅读:
    VS提示“项目文件" "已被重命名或已不在解决方案中”的解决办法 .
    微信公众平台教程和SDK收集
    “SQLServerAgent当前未运行”问题解决
    $(document).click() 在苹果手机上不能正常运行
    友盟iOS推送配置(从真机调试到推送)
    Ubuntu安装VMware Tools的方法
    TortoiseSVN客户端如何更改新的URL
    Windows Server 2008系统如何取消登录时要按Ctrl+Alt+Delete组合键
    Windows Server 2008 显示桌面图标
    用WinRAR进行安装包的制作
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5143092.html
Copyright © 2011-2022 走看看