poj1861最小生成树

#include 
#include 
#include 
#include 
#include <string>
#include 
#include 
#include 
#include 
#include <set>
#include 
#include 

using namespace std;
int father[22222];
int getfather(int x)
{
    if(x!=father[x])
        return father[x]=getfather(father[x]);
    return father[x];
}
struct Node
{
    int a;int b;int c;
}node[111111];
int cmp(const Node&a,const Node &b)
{
    return a.c<b.c;
}
int Max(int a,int b)
{
    return a>b?a:b;
}
int gg[111111];
int Map[1111][1111];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        int ans=0;
        int sum=0;
        memset(Map,0,sizeof(Map));
        for(int i=1;i<=n;i++) father[i]=i;
        for(int i=0;i){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(!Map[a][b]){
                Map[a][b]=Map[b][a]=c;
                node[ans].a=a;node[ans].b=b;node[ans++].c=c;
            }
            if(Map[a][b]&&Map[a][b]>c){
                Map[a][b]=Map[b][a]=c;
                node[ans].a=a;node[ans].b=b;node[ans++].c=c;
            }
        }
        sort(node,node+ans,cmp);
       // cout<//  for(int i=0;i//    printf("%d %d %d\n",node[i].a,node[i].b,node[i].c);
        int cc=0;
        int ret=0;
        for(int i=0;i){
            int fa=getfather(node[i].a);int fb=getfather(node[i].b);
            if(fa!=fb){
                father[fa]=fb;ret=Max(ret,node[i].c);
                gg[cc++]=i;
            }
        }
        cout<endl;
        cout<endl;
        for(int i=0;i){
            printf("%d %d\n",node[gg[i]].a,node[gg[i]].b);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/yigexigua/p/3847408.html

你可能感兴趣的:(poj1861最小生成树)