最小生成树—Kruskal模板

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef struct edge
{
	int start;
	int end;
	int weight;
}Edge;


#define MAXN 500		//点的数量
#define MAXM 25000		//边的数量

//最小生成树

class MST
{
public:
	MST(int num):m(0),n(num){
	}
	void Add_Edge(int start, int end, int weight){
		m++;
		E[m].start = start;
		E[m].end = end;
		E[m].weight = weight;
	}
	void MST_Kruskal();			//Kruskal最小生成树算法
private:
	int n;			//点的数量
	int m;			//边的数量
	Edge E[MAXN+4];
//并查集
public:
	int p[MAXN+4];
	int rank[MAXN+4];
	void Make_Set(int x);
	void Union(int x, int y);
	void Link(int x, int y);
	int Find_Set(int x);
};

//排序函数
bool cmp(Edge a, Edge b){
	return a.weight< b.weight;
}

void MST::MST_Kruskal(){
	int u,v;
	int len;
	//for each vetrex v∈V[G] do Make_Set(v)
	for(int i =1; i<=n; i++)
		MST::Make_Set(i);
	sort(E+1,E+m+1,cmp);
	for(int i=1; i<=m; i++){
		u = E[i].start, v = E[i].end;
		len = E[i].weight;
		if(Find_Set(u) != Find_Set(v)){
			//把边E[i]加入到最小生成树中
			cout<<u<<" "<<v<<" "<<len<<endl;
			Union(u,v);
		}
	}
}

//并查集操作
void MST::Make_Set(int x){
	p[x] = x;
	rank[x] = 0;
}


void MST::Union(int x, int y){
	Link(Find_Set(x),Find_Set(y));
}

void MST::Link(int x, int y){
	if(rank[x] > rank[y]){
		p[y] = x;
	}
	else{
		p[x] = y;
		if(rank[x] == rank[y])
			rank[y]++;
	}
}

int MST::Find_Set(int x){
	if(x != p[x])
		p[x] = Find_Set(p[x]);
	return p[x];
}

#define UNSUBMIT
int main(){
#ifdef UNSUBMIT
	freopen("a.in","r",stdin);
#endif
	int n,m;				//输入点,边的数量
	int start,end,weight;
	scanf("%d%d",&n,&m);
	MST *t = new MST(n);
	for(int i=1; i<=m; i++){
		scanf("%d%d%d",&start,&end,&weight);
		t->Add_Edge(start,end,weight);
	}
	printf("最小生成树为\n");
	t->MST_Kruskal();
	return 0;
}

测试数据
9 14
1 2 4
1 8 8
2 9 11
2 3 8
9 8 7
8 7 1
9 7 6
9 3 2
7 6 2
3 6 4
3 4 7
4 5 9
4 6 14
6 5 10 
非常感谢 windmissing   http://blog.csdn.net/mishifangxiangdefeng/article/details/7843883

在阅读算法导论的道路上,给予我很多帮助

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