poj1251解题报告

题目大意:给你一个AOE网络,表示一个森林,有结点权(村庄编号),也有边权(费用),保持森林中每个村子都有道路相通,且维修所需要的最少money。

解题思路:MST最小生成树,kruska算法~~

没什么要注意的,我的算法改自刘汝佳的模板

#include
#include
#include
#include
using namespace std;
const int maxn=1000;
int u[maxn],v[maxn],w[maxn],p[maxn],r[maxn],n,m;
int cmp(const int i,const int j) {return w[i] int find(int x) {return p[x]==x?x:p[x]=find(p[x]);}
int kruskal()
{
    int cou=0,x,y,i,ans=0;
    for(i=0;i     for(i=0;i     sort(r,r+m,cmp);
    for(i=0;i     {
        int e=r[i];x=find(u[e]);y=find(v[e]);
        if(x!=y) {ans += w[e];p[x]=y;cou++;}
    }
    if(cou     return ans;
}
int main()
{
        //freopen("1.txt","r",stdin);
        while(cin>>n)
        {
                m=0;
                if(n==0) break;
                for(int i =0;i                 {
                        char a;
                        cin>>a;
                        int tmp;
                        cin>>tmp;
                        for(int j=0;j                         {
                                char b;
                                cin>>b;
                                int tmp1;
                                cin>>tmp1;
                                u[m]=a-'A';
                                v[m]=b-'A';
                                w[m++]=tmp1;
                        }
                }
               cout<         }
}


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