两种构造最小生成树的算法(普里姆算法,克鲁斯卡尔算法)

(一)普里姆算法

 

普里姆算法求最小生成树:从生成树中只有一个顶点开始,到定点全部进入生成数为止;

 

 

2.克鲁斯卡尔算法。

思想:将所有边按其权值从小到大排一遍,从小到大依次选取边,加入最小生成树中,若加入此边后不会产生回路,则将此边加入,否则则舍去,遍历一遍所有的边。可通过并查集判断是否构成回路。

 

例题:HDU1863 畅通工程

题意:

  省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。

Input

测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N 
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。 

Output

对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。

Sample Input

3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100

Sample Output

3
?

显然是个最小生成数的题。

附上克鲁斯卡尔算法的代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#define ll long long
using namespace std;
int n,m;
int a[105];
struct node{
	int p,q;
	int z;
}g[10010];
bool cmp(node p,node q)
{
	return p.z <q.z ;
}
void init()
{
	int i;
	for(i=0;i<105;i++)
	    a[i]=i;
}
int find(int x)
{
	if(x==a[x])return a[x];
	return find(a[x]);
}
int Union(int x,int y)
{
	int xx=find(x);
	int yy=find(y);
	if(xx==yy)return 0;
	a[xx]=yy;
	return 1;
}
int main()
{
	while(1)
	{
		init();
		scanf("%d%d",&n,&m);
		if(n==0)break;
		int i,j,k,s,t;
		for(i=0,j=0;i<n;i++)
		{
			scanf("%d%d%d",&s,&k,&t);
			g[j].p =s;g[j].q =k;
			g[j].z =t;j++;
			g[j].p =k;g[j].q =s;
			g[j].z =t;j++;
		 } 
		 sort(g,g+j,cmp);//按照权值排序 
		 int ans=0;
		for(i=0,k=0;i<j;i++)
		{
			if(Union(g[i].p ,g[i].q ))//利用并查集判断是否构成回路
			{
				ans=ans+g[i].z ;k++;
			}
		}
		if(k!=m-1)
		{
			printf("?\n");
		}
		else printf("%d\n",ans);
	}
    return 0;
}

附上普里姆算法代码:(一般不用)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#define Max 99999999
#define ll long long
using namespace std;
int n,m,sum; 
int a[105][105];
void init()
{
	int i,j;
	for(i=0;i<=m;i++)
	{
		a[i][i]=0;
		for(j=0;j<i;j++)
		{
			a[i][j]=a[j][i]=Max;
		}
	}
}
bool prim()
{
	int mp[105]={0};
	int i,j,k;
	int dis[105];
	for(i=0;i<=m;i++)
	{
		dis[i]=a[1][i];
	}
	mp[1]=1;
	 sum=0;
	 int z=0;
	for(i=1;i<=m;i++)
	{
		int ans=Max;
		for(j=1;j<=m;j++)
		{
			if(ans>dis[j]&&mp[j]==0)
			{
				ans=dis[j];
				k=j;
			}
		}
		mp[k]=1;
		if(ans!=Max)
		{
			sum=sum+ans;z++;
		}
		for(j=1;j<=m;j++)
		{
			if(dis[j]>a[k][j]&&mp[j]==0)
			{
				dis[j]=a[k][j];
			}
		}
	}
	if(z==m-1)return true;//最小生成树有m-1条边
	return false;
}
int main()
{
	while(1)
	{
		scanf("%d%d",&n,&m);
		if(n==0)break;
		init();
		int i,j,k,l;
		for(i=0;i<n;i++)
		{
			scanf("%d%d%d",&j,&k,&l);
			a[j][k]=a[k][j]=l;
		}
		if(prim())
		{
			printf("%d\n",sum);
		 } 
		 else printf("?\n");
	}
    return 0;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是最小生成树普里姆算法克鲁斯卡尔算法的C语言实现: 1. 普里姆算法 ```c #include <stdio.h> #include <limits.h> #define V 5 int minKey(int key[], bool mstSet[]) { int min = INT_MAX, min_index; for (int v = 0; v < V; v++) if (mstSet[v] == false && key[v] < min) min = key[v], min_index = v; return min_index; } void printMST(int parent[], int graph[V][V]) { printf("Edge \tWeight\n"); for (int i = 1; i < V; i++) printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]); } void primMST(int graph[V][V]) { int parent[V]; int key[V]; bool mstSet[V]; for (int i = 0; i < V; i++) key[i] = INT_MAX, mstSet[i] = false; key[0] = 0; parent[0] = -1; for (int count = 0; count < V - 1; count++) { int u = minKey(key, mstSet); mstSet[u] = true; for (int v = 0; v < V; v++) if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v]) parent[v] = u, key[v] = graph[u][v]; } printMST(parent, graph); } int main() { int graph[V][V] = { { 0, 2, 0, 6, 0 }, { 2, 0, 3, 8, 5 }, { 0, 3, 0, 0, 7 }, { 6, 8, 0, 0, 9 }, { 0, 5, 7, 9, 0 } }; primMST(graph); return 0; } ``` 2. 克鲁斯卡尔算法 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define V 5 #define E 9 struct Edge { int src, dest, weight; }; struct Graph { int V, E; struct Edge* edge; }; struct Graph* createGraph(int V, int E) { struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph)); graph->V = V; graph->E = E; graph->edge = (struct Edge*)malloc(graph->E * sizeof(struct Edge)); return graph; } struct subset { int parent; int rank; }; int find(struct subset subsets[], int i) { if (subsets[i].parent != i) subsets[i].parent = find(subsets, subsets[i].parent); return subsets[i].parent; } void Union(struct subset subsets[], int x, int y) { int xroot = find(subsets, x); int yroot = find(subsets, y); if (subsets[xroot].rank < subsets[yroot].rank) subsets[xroot].parent = yroot; else if (subsets[xroot].rank > subsets[yroot].rank) subsets[yroot].parent = xroot; else { subsets[yroot].parent = xroot; subsets[xroot].rank++; } } int myComp(const void* a, const void* b) { struct Edge* a1 = (struct Edge*)a; struct Edge* b1 = (struct Edge*)b; return a1->weight > b1->weight; } void KruskalMST(struct Graph* graph) { int V = graph->V; struct Edge result[V]; int e = 0; int i = 0; qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp); struct subset* subsets = (struct subset*)malloc(V * sizeof(struct subset)); for (int v = 0; v < V; ++v) { subsets[v].parent = v; subsets[v].rank = 0; } while (e < V - 1 && i < graph->E) { struct Edge next_edge = graph->edge[i++]; int x = find(subsets, next_edge.src); int y = find(subsets, next_edge.dest); if (x != y) { result[e++] = next_edge; Union(subsets, x, y); } } printf("Following are the edges in the constructed MST\n"); for (i = 0; i < e; ++i) printf("%d -- %d == %d\n", result[i].src, result[i].dest, result[i].weight); return; } int main() { int V = 5; int E = 9; struct Graph* graph = createGraph(V, E); graph->edge[0].src = 0; graph->edge[0].dest = 1; graph->edge[0].weight = 2; graph->edge[1].src = 0; graph->edge[1].dest = 3; graph->edge[1].weight = 6; graph->edge[2].src = 1; graph->edge[2].dest = 2; graph->edge[2].weight = 3; graph->edge[3].src = 1; graph->edge[3].dest = 4; graph->edge[3].weight = 5; graph->edge[4].src = 2; graph->edge[4].dest = 4; graph->edge[4].weight = 7; graph->edge[5].src = 3; graph->edge[5].dest = 4; graph->edge[5].weight = 9; KruskalMST(graph); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值