P3366 kruskal template [Title]

#include <the iostream> 
#include <algorithm>
 the using  namespace STD;
 const  int MAXM = 100000 ;
 const  int MAXN = 5000 ;
 int TOT; 

struct Edge {
     int U, V, W; 
} E [MAXM * 2 ]; 

int Father [ MAXN];
 void the make ( int n-) {
     for ( int I = 0 ; I <n-; I ++)     // initialize 
        father [I] = I;     // make the father of each element is the element itself 
} 

intFind ( int x) {
     IF (Father x = [x]!)     // if the father is not itself, the ancestor of x to find 
        Father [x] = Find (Father [x]);     // by looking father element x the father found ancestor 
    return father [x];             // returns the ancestor of x 
} 

int CMP (Edge a, Edge B) {
     return AW < BW; 
} 

void Work ( int I) { 
    TOT = TOT + E [I] .W ; 
} 

BOOL Kruskal ( int n-, int m) {
     int CNT = 0 ; 
    the make (n-); 
    Sort (E, E +m,cmp);
    for(int i=1;i<=m;i++)
    {
        int u=e[i].u,v=e[i].v;
        if(find(u)!=find(v))
        {
            father[find(u)]=find(v);
            work(i);
            cnt++;
        }
        if(cnt==n-1) break;
    }
    if(cnt!=n-1) return false;
    return true;
}

int main(){
    int N,M;
    cin>>N>>M;
    for(int i=1;i<=M;i++){
        int X,Y,Z;
        cin>>X>>Y>>Z;
        e[i].u=X,e[i].v=Y,e[i].w=Z;
    }
    if(kruskal(N,M)) cout<<tot<<endl;
    else cout<<"orz"<<endl;    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/kohano/p/11745680.html
Recommended