(Prime算法)Agri-Net(12.2.1)

 

Description

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.

Input

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Sample Input

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28

 

第一种方法还是使用了Kruskal算法,但是这次的话如果是直接使用Kruskal算法的话,就会超时了,这样的话在

#include<iostream>
#include <algorithm>
using namespace std;

int fa[120];
int get_father(int x)
{
	
	if(fa[x]==x)
		return x;
	else
		return  fa[x]=get_father(fa[x]);
	//	return fa[x]=fa[x]==x ?x:get_father(fa[x]);//判断两个节点是否属于一颗子树(并查集)
}
int main()
{
	int temp[100005],tem=0;
	int n;
	int p[120][120];
	while(scanf("%d",&n)!=EOF)
	{
		tem=0;
		int i,j,k,m;
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
			{
				scanf("%d",&p[i][j]);
			    temp[ p[i][j] ]=1;        //记录下在两个相邻的长度中有这个值
				                           //其实这个步骤可以没有,因为设这个变量就是为了在后面遍历查找的时候会快一点,
				                          //在此处呢能都显示除出设出此变量会加快运算速度,如果没有这个约束条件就会超时,自己试验过
			}
		for(i=0;i<n;i++)
			fa[i]=i;
		int ans=0;
		for(k=0;k<=100000;k++)// kruskal 算法
		{
			if(temp[k]==1)//如果这个长度在路径中存在,如果直接没有当前距离的话就没有必要查找了
			{
			for(i=0;i<n;i++)
				for(int j=0;j<n;j++)
					if(p[i][j]==k && get_father(i)!=get_father(j))//当出现当前距离并且i和j未连接时,合并两个集合
					{
						fa[fa[i]]=fa[j];               //合并两颗子树(并查集)
						ans+=k;
					}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


经典的Prim算法:

//prim算法
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;

int n;
int tot;
int v[150][150];
int dist[150];//存 节点到树 的最小距离
bool use[150];//标记节点是否存在

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		memset(use,false,sizeof(use));//初始化节点 都存在
		tot=0;
		use[0]=1;
		int i,j,k;
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				scanf("%d",&v[i][j]);
			dist[0]=0x7FFFFFFF;//赋值为最大值
		for(i=1;i<n;i++)//初始距离
				dist[i]=v[0][i];
		for(i=1;i<n;i++)
		{                   //连接 剩下的 n-1个节点
			int tmp=0;
			for(k=1;k<n;k++)//找到最短的距离 节点
				if(dist[k]<dist[tmp] &&!use[k])
					tmp=k;
			tot +=dist[tmp];// 加到 总距离中
			use[tmp]=true;
			for(k=1;k<n;k++)//调整距离
				if(!use[k])
					dist[k]=v[k][tmp]<dist[k]?v[k][tmp]:dist[k];
		}
		printf("%d\n",tot);
	}
	return 0;
}



 

 

 

 

 

 

你可能感兴趣的:((Prime算法)Agri-Net(12.2.1))