最小生成树(Prim算法和Kruskal算法)

最小生成树:一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边。


概述:

在一给定的无向图G = (V, E) 中,(u, v) 代表连接顶点 u 与顶点 v 的边(即),而 w(u, v) 代表此边的权重,若存在 T 为 E 的子集(即)且为无循环图,使得 w(T) 最小,则此 T 为 G 的最小生成树。最小生成树其实是最小权重生成树的简称。[1] 


Prim算法:(稠密图使用效率比较高)

const int maxn = 105;
int T, n, m;
int map[maxn][maxn];
bool vis[maxn];//标记数组
int low[maxn];//i点到邻近点(未标记)的最小距离

int Prim(){
	int ans=0,pos,min;
	MEM1(vis);
	//从某点开始,分别标记和记录该点
	vis[1]=1;pos=1;
	//第一次给low[]赋值
	for2(i,1,n){
		if(i!=pos)
			low[i]=map[pos][i];
	}

	for1(i,1,n){//找最小值并记录位置
		min=INT_MAX;
		for2(j,1,n){
			if(!vis[j]&&min>low[j]){
				min=low[j];
				pos=j;
			}
		}
		ans+=min;
		vis[pos]=1;

		for2(j,1,n){
			if(!vis[j]&&low[j]>map[pos][j])
				low[j]=map[pos][j];
		}
	}

	return ans;
}


Kruskal算法:

1.将图各边按照权值进行排序

2.将图遍历一次,找出权值最小的边,(条件:此次找出的边不能和已加入最小生成树集合的边构成环),若符合条件,则加入最小生成树的集合中。不符合条件则继续遍历图,寻找下一个最小权值的边。

3.递归重复步骤1,直到找出n-1条边为止(设图有n个结点,则最小生成树的边数应为n-1条),算法结束。得到的就是此图的最小生成树。


const int maxn=100010;
int T,n,m;
int father[maxn],r[maxn];

struct kruskal{
	int a;//边的起点
	int b;//边的终点
	int value;//边的权值
}Edge[maxn];

bool cmp(kruskal a,kruskal b){
	return a.value<b.value;//按权值排序
}

//并查集
void Make_Set(int x){
	father[x]=x;
	r[x]=1;
}

int Findfather(int x){
	return x==father[x]?x:Findfather(father[x]);
}

bool Union(int a,int b){
	a=Findfather(a);
	b=Findfather(b);
	if(a==b)
		return false;
	else{
		if(r[a]>r[b]){
			father[b]=a;
			r[a]+=r[b];
		}
		else{
			father[a]=b;
			r[b]+=r[a];
		}
	}
	return true;
}



int main(){
#ifndef ONLINE_JUDGE
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
#endif
	while(~sf(n)){
		if(!n)
			break;
		for2(i,0,n){
			Make_Set(i);
		}
		for1(i,0,n*(n-1)/2){
			scanf("%d%d%d",&Edge[i].a,&Edge[i].b,&Edge[i].value);
		}
		sort(Edge,Edge+n*(n-1)/2,cmp);
		int lnum=0,sum=0;
		for1(i,0,n*(n-1)/2){
			if(Union(Edge[i].a,Edge[i].b)){
				lnum++;
				sum+=Edge[i].value;
			}
			if(lnum==n-1){//边数=顶点数-1
				break;
			}
		}
		pf(sum);
	}
	return 0;
}


</pre><pre name="code" class="cpp">例题:hdu 1233还是畅通工程的Prim和Kruskal两种AC代码
<span style="font-family: Arial, Helvetica, sans-serif;">Prim:</span>
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<memory.h>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)                          //[i,a);
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
using namespace std;

template<class T>
T Mint(T a, T b, T c) {
	if (a>b) {
		if (c>b)
			return b;
		return c;
	}
	if (c>a)
		return a;
	return c;
}

template<class T>
T Maxt(T a, T b, T c) {
	if (a>b) {
		if (c>a)
			return c;
		return a;
	}
	else if (c > b)
		return c;
	return b;
}

const int maxn=10010;
int T,n,m;
int a,b,c;
int map[maxn][maxn],low[maxn];
bool vis[maxn];

int Prim(){
	MEM1(vis);
	int ans=0,pos=1;
	vis[1]=1;
	for(int i=1;i<=n;i++){
		if(i!=pos)
			low[i]=map[pos][i];
	}
	for(int i=1;i<n;i++){
		int min=INT_MAX;
		for(int j=1;j<=n;j++){
			if(!vis[j]&low[j]<min){
					min=low[j];
					pos=j;
			}
		}
		vis[pos]=1;
		for(int j=1;j<=n;j++){
			if(!vis[j]&&low[j]>map[pos][j])
				low[j]=map[pos][j];
		}
		ans+=min;
	}
	return ans;
}


int main(){
#ifndef ONLINE_JUDGE
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
#endif
	while(~sf(n)){
		if(!n)
			break;
		for1(i,0,n*(n-1)/2){
			scanf("%d%d%d",&a,&b,&c);
			map[a][b]=map[b][a]=c;
		}
		int ans=Prim();
		pf(ans);
	}
	return 0;
}


 
Kruskal:
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<memory.h>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)                          //[i,a);
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
using namespace std;

template<class T>
T Mint(T a, T b, T c) {
	if (a>b) {
		if (c>b)
			return b;
		return c;
	}
	if (c>a)
		return a;
	return c;
}

template<class T>
T Maxt(T a, T b, T c) {
	if (a>b) {
		if (c>a)
			return c;
		return a;
	}
	else if (c > b)
		return c;
	return b;
}

const int maxn=100010;
int T,n,m;
int father[maxn],r[maxn];

struct kruskal{
	int a;//边的起点
	int b;//边的终点
	int value;//边的权值
}Edge[maxn];

bool cmp(kruskal a,kruskal b){
	return a.value<b.value;//按权值排序
}

//并查集
void Make_Set(int x){
	father[x]=x;
	r[x]=1;
}

int Findfather(int x){
	return x==father[x]?x:Findfather(father[x]);
}

bool Union(int a,int b){
	a=Findfather(a);
	b=Findfather(b);
	if(a==b)
		return false;
	else{
		if(r[a]>r[b]){
			father[b]=a;
			r[a]+=r[b];
		}
		else{
			father[a]=b;
			r[b]+=r[a];
		}
	}
	return true;
}



int main(){
#ifndef ONLINE_JUDGE
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
#endif
	while(~sf(n)){
		if(!n)
			break;
		for2(i,0,n){
			Make_Set(i);
		}
		for1(i,0,n*(n-1)/2){
			scanf("%d%d%d",&Edge[i].a,&Edge[i].b,&Edge[i].value);
		}
		sort(Edge,Edge+n*(n-1)/2,cmp);
		int lnum=0,sum=0;
		for1(i,0,n*(n-1)/2){
			if(Union(Edge[i].a,Edge[i].b)){
				lnum++;
				sum+=Edge[i].value;
			}
			if(lnum==n-1){//边数=顶点数-1
				break;
			}
		}
		pf(sum);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值