Poj 1258 Agri-Net【最小生成树】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuke19950717/article/details/47450911

Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 45255   Accepted: 18591

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


题意:

若干个顶点,给出每个顶点连接其他各个顶点的花费,问实现所有顶点相互连接,最少的花费


题解:

最小生成树,用的kruscal算法,先统计出相应的边,然后再排序,从最小权值的边入手,加入集合,判断成功加入时,就累加总权值,最好当有n-1条边时,最小生成树结束,输出相应的结果...


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,cnt,kase,per[105];
struct lu
{
	int a,b;
	int len;
}x[10005];
void init()
{
	for(int i=1;i<=n;++i)
	{
		per[i]=i;
	}
}
int find(int x)
{
	int r=x;
	while(r!=per[r])
	{
		r=per[r];
	}
	int i=x,j;
	while(i!=r)
	{
		j=per[i];per[i]=r;i=j;
	}
	return r;
}
void join(int x,int y)
{
	int fx=find(x),fy=find(y);
	if(fx!=fy)
	{
		per[fx]=fy;
		++cnt;kase=1;
	}
}
int cmp(lu a,lu b)
{
	return a.len<b.len;
}
int main()
{
	int t,i,j,a,c,m;
	while(~scanf("%d",&n))
	{
		init();c=m=cnt=0;//注意初始化
		for(i=0;i<n;++i)
		{
			for(j=0;j<n;++j)
			{
				scanf("%d",&a);
				if(i!=j)//统计顶点和边
				{
					x[c].a=i+1;x[c].b=j+1;
					x[c++].len=a;
				}
			}
		}
		sort(x,x+c,cmp);
		for(i=0;cnt<n-1;++i)//遍历查找,加入集合
		{
			kase=0;
			join(x[i].a,x[i].b);
			if(kase)
			{
				m+=x[i].len;//累加总权值
			}
		}
		printf("%d\n",m);
	}
	return 0;
}


重新写了一遍更优化写的代码.....


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,per[105];
struct lu
{
	int a,b,len;
}x[5005];
void init()
{
	for(int i=1;i<=n;++i)
	{
		per[i]=i;
	}
}
int find(int x)
{
	return x==per[x]?x:per[x]=find(per[x]);//递归压缩,更简短 
	/*int r=x;
	while(r!=per[r])
	{
		r=per[r];
	}
	int i=x,j;
	while(i!=r)//循环用的多了,偶尔试试递归
	{
		j=per[i];per[i]=r;i=j;
	}
	return r;*/
}
int join(int x,int y)
{
	int fx=find(x),fy=find(y);
	if(fx!=fy)
	{
		per[fx]=fy;
		return 1; 
	}
	return 0;
}
bool cmp(lu a,lu b)
{
	return a.len<b.len;
}
int main()
{
	int i,j,a,k;
	while(~scanf("%d",&n))
	{
		init();k=0;
		for(i=0;i<n;++i)
		{
			for(j=0;j<n;++j)
			{
				scanf("%d",&a);
				if(i<j)//这个限定条件,使得保存的边没有重复的
				{
					x[k].a=i+1;x[k].b=j+1;
					x[k++].len=a;
				}
			}
		}
		sort(x,x+k,cmp);
		int sum=0,cnt=0;
		for(i=0;cnt<n-1;++i)//这样限定,提前跳出循环
		{
			if(join(x[i].a,x[i].b))
			{
				sum+=x[i].len;
				++cnt;//边数
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}


// 2016年4月13日

不知道什么鬼,用prim 算法死活过不去,一直显示TLE,看到大神们使用的也是裸的prim啊,看来大神毕竟是大神........

最后终于发现问题了,终止格式错误,但是为什么不是WA,却是TLE啊!!!!



/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=105;
int graph[maxn][maxn],mincost[maxn];
void init(int n)
{
	memset(graph,inf,sizeof(graph));
	memset(mincost,inf,sizeof(mincost));
}
int prim(int n)
{
	int vis[maxn]={0},ans=0;
	mincost[0]=0;
	while(1)
	{
		int v=-1;
		for(int u=0;u<n;++u)
		{
			if(!vis[u]&&(v==-1||mincost[u]<mincost[v]))
			{
				v=u;
			}
		}
		if(v==-1)
		{
			break;
		}
		vis[v]=1;
		ans+=mincost[v];
		for(int u=0;u<n;++u)
		{
			if(!vis[u])
			{
				mincost[u]=min(mincost[u],graph[v][u]);
			}
		}
	}
	return ans;
}
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		init(n);
		for(int i=0;i<n;++i)
		{
			for(int j=0;j<n;++j)
			{
				scanf("%d",&graph[i][j]);
			}
		}
		printf("%d\n",prim(n));
	}
	return 0;
}


阅读更多
换一批

没有更多推荐了,返回首页