如题,谢谢了!!!

解决方案 »

  1.   

    #include <iostream>
    #include <queue>
    using namespace std;
    const long Max=101;
    struct node
    {
        long from;
        long to;
        long cost;
    };
    int set[Max];
    priority_queue<node> q;
    bool operator <(const node &a, const node &b)
    {
        return a.cost>b.cost;
    }long find(long x)
    {
        long i=x,r=x,j;
        while(set[r]!=r)
            r=set[r];
        while(i!=r)
        {
            j=set[i];
            set[i]=r;
            i=j;
        }
        return r;
    }
    void merge(long a,long b)
    {
        long x,y;
        x=find(a);
        y=find(b);
        if(x!=y)
            set[x]=y;
    }long Kruskal()
    {
        long rs=0,i;
        node e;
        for(i=0;i<Max;i++)
            set[i]=i;
        while(!q.empty())
        {
            e=q.top();
            q.pop();
            if(find(e.from)!=find(e.to))
            {
                merge(e.from,e.to);
                rs+=e.cost;
            }
        }
        return rs;
    }
    int main()
    {
        /**//*
            while(!q.empty())  //清空
            {
                q.pop();
            }
            while(m--)     //边数
            {
                scanf("%ld%ld%ld",&e.from,&e.to,&e.cost);
                q.push(e);
                swap(e.from,e.to);//无向图
                q.push(e);
            }
            rs=Kruskal();
            printf("%ld\n",rs);
        }
        return 0;*/
    }
      

  2.   

    C++很好实现,资料很多。Java就难求了~