Kruskal MST

《挑战程序设计竞赛》,初级篇–图

// Kruskal  O(E*logV)
// Minimum Spanning Trees
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define maxv 101
#define maxe 101
#define INF 9999
/* union_find sets */

int par[maxv];
int rank[maxv];

void init(int n)
{
    for(int i = 0; i < n; i++)
    {
        par[i] = i;
        rank[i] = 0;
    }
}

// 查询树的根
int find(int x)
{
    if(par[x] == x)
        return x;
    else 
        return par[x] = find(par[x]);
}

// 合并x和y所属的集合
void unite(int x, int y)
{
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(rank[x] < rank[y])
        par[x] = y;
    else
        par[y] = x;
        if(rank[x] == rank[y]) rank[x]++;
}

bool same(int x,int y)
{
    return find(x) == find(y);
}

/* Kruskal */
struct edge
{
    int u,v;
    int cost;
};

bool cmp(const edge& e1, const edge& e2)
{
    return e1.cost < e2.cost;
}

edge es[maxe];
int V,E;

int Kruskal()
{
    sort(es, es+E, cmp);
    init(V);
    int res = 0;
    for(int i = 0; i < E; i++)
    {
        edge e = es[i];
        if(!same(e.u,e.v))
        {
            unite(e.u,e.v);
            res += e.cost;
        }
    }
    return res;
}

int main()
{
    cin>>V>>E;
    for(int i = 0; i < E; i++)
    {
        edge e;
        cin>>e.u>>e.v>>e.cost;
        es[i] = e;
    }
    cout<<"----------------"<<endl;
    cout<<"最少花费为:"<<Kruskal();

    return 0;
}
/*
测试数据:
5 6
0 1 1
0 2 3
1 2 4
1 4 6
2 3 2
2 4 1
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值