首页 > 其他 > 详细

HDU 1233 还是畅通工程。

时间:2017-03-04 15:13:48      阅读:237      评论:0      收藏:0      [点我收藏+]

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1233

技术分享

技术分享x

解题思路:

简单的最小生成树

实现代码:

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

const int maxn=101;


struct NodeHeap{
    int x,y, w;
    bool operator <(const NodeHeap & rhs)const{
        return w<rhs.w;
    }
}edges[maxn*maxn];

int f[maxn];

int findfa(int u){
    if(f[u]==u) return u;
    return f[u]=findfa(f[u]);
}

void unit(int u,int v){
    int fu=findfa(u);
    int fv=findfa(v);
    if(fu!=fv)
        f[fu]=fv;
}

int main(){
    int N;
    while(scanf("%d",&N)!=EOF&&N!=0){

        for(int i=0;i<N*(N-1)/2;i++){
            scanf("%d%d%d",&edges[i].x,&edges[i].y,&edges[i].w);
        }
        sort(edges,edges+(N*(N-1)/2));

        for(int i=1;i<=N;i++){
            f[i]=i;
        }

        long long cost=0;
        int k=0;
        for(int i=0;i<(N*(N-1)/2);i++){

            if(findfa(edges[i].x)!=findfa(edges[i].y)){
                unit(edges[i].x,edges[i].y);
                cost+=edges[i].w;
                k++;

            }

            if(k==N-1){
                break;
            }
        }

        printf("%d\n",cost);
    }
    return 0;

}

 

HDU 1233 还是畅通工程。

原文:http://www.cnblogs.com/IKnowYou0/p/6501405.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!