夜雨离殇吧 关注:1,669贴子:89,273
  • 14回复贴,共1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
#define MAXM 100000
#define MAXN 10000
#define LL long long int
struct edge
{
     int leftnode;
     int rightnode;
     int data;
     edge();
};
edge::edge()
{
     leftnode=0;
     rightnode=0;
     data=0;
}
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
edge a[MAXM+1];
int n,m,father[MAXN+1],times=0;
LL ans=0;
int getfather(int x)
{
     if(father[x]==x)
        return x;
     else
        return father[x]=getfather(father[x]);
}
void hebin(int x,int y)
{
     father[getfather(x)]=father[getfather(y)];
}
int cmp(const void *a,const void *b)
{
     return (*(edge *)a).data-(*(edge *)b).data;
}
int main()
{    
     freopen("kruskal.in","r",stdin);
     freopen("kruskal.out","w",stdout);
     cin>>n>>m;
     for(int i=0;i<m;i++)
     {
         cin>>a[i].leftnode>>a[i].rightnode>>a[i].data;
     }
     qsort(a,m,sizeof(edge),cmp);
     int t=0;
     while(times!=n-1)
     {
         if(getfather(a[t].leftnode)!=getfather(a[t].rightnode))
         {
             ans+=a[t].data;
             hebin(a[t].leftnode,a[t].rightnode);
             times++;
         }
         t++;
     }
     cout<<ans<<endl;
}



1楼2010-12-31 14:25回复
    测试一下['[m 5GYkhz-?Pbt'9K]o"3EWi]


    2楼2011-01-04 16:09
    回复
      测试一下[^M^p


      3楼2011-01-04 16:09
      回复
        OKNQ#TWbZR]`1c!fpi`lOor/u


        4楼2011-01-07 22:26
        回复
          \_bdhDn$qtwRz}!"*P-0,69


          5楼2011-01-07 22:26
          回复
            P#


            6楼2011-01-07 22:28
            回复
              /@CFIeLOR5UX[c^ad3gjmapsA


              7楼2011-01-07 22:28
              回复
                z3*69i<Y?HBE(HwKgNWQFT6WZ


                8楼2011-01-07 22:29
                回复
                  *147F:=


                  9楼2011-01-07 22:29
                  回复
                    向众神努力~


                    10楼2011-01-08 00:38
                    回复
                      【test】


                      11楼2011-01-10 11:06
                      回复
                        720327


                        12楼2011-01-15 22:51
                        回复
                          你妹的.看不懂
                          我爱saber


                          IP属地:黑龙江14楼2011-10-19 00:52
                          回复
                            你大半夜抽什么疯= =


                            15楼2011-10-19 01:43
                            回复
                              我抽风我自豪
                              我爱saber


                              IP属地:黑龙江16楼2011-10-19 08:54
                              回复