最小生成树kruskal算法 适合稀疏图

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 1000
int father[MAX], son[MAX];
int v, l;

typedef struct Kruskal //存储边的信息
{
    int a;
    int b;
    int value;
};

bool cmp(const Kruskal & a, const Kruskal & b)
{
    return a.value < b.value;
}

int unionsearch(int x) //查找根结点+路径压缩
{
    return x == father[x] ? x : unionsearch(father[x]);
}

bool join(int x, int y) //合并
{
    int root1, root2;
    root1 = unionsearch(x);
    root2 = unionsearch(y);
    if(root1 == root2) //为环
        return false;
    else if(son[root1] >= son[root2])//谁儿子多往谁哪里靠
    {
        father[root2] = root1;
        son[root1] += son[root2];
    }
    else
    {
        father[root1] = root2;
        son[root2] += son[root1];
    }
    return true;
}

int main()
{
    int ncase, ltotal, sum, flag;
    Kruskal edge[MAX];
    scanf("%d", &ncase);
    while(ncase--)
    {
        scanf("%d%d", &v, &l);
        ltotal = 0, sum = 0, flag = 0;
        for(int i = 1; i <= v; ++i) //初始化
        {
            father[i] = i;
            son[i] = 1;
        }
        for(int i = 1; i <= l ; ++i)
        {
            scanf("%d%d%d", &edge[i].a, &edge[i].b, &edge[i].value);
        }
        sort(edge + 1, edge + 1 + l, cmp); //按权值由小到大排序
        for(int i = 1; i <= l; ++i)
        {
            if(join(edge[i].a, edge[i].b))
            {
                ltotal++; //边数加1
                sum += edge[i].value; //记录权值之和
                //cout<<edge[i].a<<"->"<<edge[i].b<<endl;
            }
            if(ltotal == v - 1) //最小生成树条件:边数=顶点数-1
            {
                flag = 1;
                break;
            }
        }
        if(flag) printf("%d\n", sum);
        else printf("data error.\n");
    }
    return 0;
}

posted on 2016-04-14 16:22  very_czy  阅读(544)  评论(0编辑  收藏  举报

导航