`
Simone_chou
  • 浏览: 184667 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Agri-Net(kruskal)

 
阅读更多
H - Agri-Net
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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

    题意:

    给出N(3到100)个村庄数,输入N*N矩阵,a[ i ][ j ]代表 i 村庄到 j 村庄的距离,求能到达所有村庄的最短路径。

    题意:

    用kruskal算法求最小生成树。

    AC:

#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct
{
  int from,to,len;	
}r;
r road[5000];
//主要路径数组的大小
int root[110];

int cmp(r a,r b)
{
	return a.len<b.len;
}

int find(int a)
{
	int x=a,t;
	while(root[x]!=x)
	        x=root[x];
	while(x!=a)
	   {
		t=root[a];
		root[a]=x;
		a=t;
	   }
	return x;
}

int main()
{
	int n,i,j,sum=0,length;
	int pos[110][110];
	while(scanf("%d",&n)!=EOF)
	{
	  for(i=1;i<=n;i++)
	     root[i]=i;
	  memset(pos,0,sizeof(pos));
	  for(i=1;i<=n;i++)
	     for(j=1;j<=n;j++)
	        scanf("%d",&pos[i][j]);
	  sum=0;
	  for(i=1;i<n;i++)
	     for(j=i+1;j<=n;j++)
	     {
	  	sum++;
	  	road[sum].from=i;
	  	road[sum].to=j;
	  	road[sum].len=pos[i][j];
	     }
	  sort(road+1,road+sum+1,cmp);
	  length=0;
	  for(i=1;i<=sum;i++)
	    {
	        int fa,fb;
		fa=find(road[i].from);
		fb=find(road[i].to);
		if(fa==fb) continue;
		else
		{
		   root[fa]=fb;
		   length+=road[i].len;
		}
	     }
	  printf("%d\n",length);
        }
	return 0;
}

   总结:

   road数组是用来装路径的起点终点和长度的,所以路径条数应该不止顶点数那么多而已。开4000一下就会RE,开5000的时候就AC了,开少于1000的时候就TLE,所以应该要尽量开大点。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics