zoukankan      html  css  js  c++  java
  • hdu1863

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int N,M;
    struct edge { int u,v,cost;
        bool operator < (const edge& rhs) const
        {
            return cost < rhs.cost;
        }
    
    };
    edge es[110];
    int sett[110];
    int find2(int x)
    {
        if(sett[x] == x ) return x;
        else return sett[x] = find2(sett[x]);
    }
    void unite(int x,int y)
    {
        x =find2(x);
        y =find2(y);
        if(x<y) sett[y]=x;
        else sett[x]=y;
    }
    bool same(int x,int y)
    {
        return find2(x)==find2(y);
    }
    int kruskal()
    {
        sort(es,es+N);
        for(int i=0;i<=M;i++) sett[i]=i;
        int res=0;
        int num=0;
        for(int i=0;i<N;i++){
            edge e = es[i];
            if( !same(e.u, e.v) )
            {
                unite(e.u,e.v);
                res+=e.cost;
                num++;
            }
        }
    //    printf("%d%d",num,M);
        if(num < M-1) printf("?
    ");
        else printf("%d
    ",res);
    }
    int main()
    {
        while(scanf("%d%d",&N,&M)&&N )
        {
            for(int i=0;i<N;i++)
                scanf("%d%d%d",&es[i].u,&es[i].v,&es[i].cost);
            kruskal();
        }
        return 0;
    }
    

  • 相关阅读:
    jsp实现登陆功能小实验
    netty
    shiro
    mybatis
    spring MVC
    spring
    集合框架面试题
    Redis面试题
    Dubbo面试题汇总
    阿里面试题
  • 原文地址:https://www.cnblogs.com/llguanli/p/8495113.html
Copyright © 2011-2022 走看看