Agri-Net
| Time Limit:1000MS | Memory Limit:10000K | |
| Total Submissions:17635 | Accepted:7091 |
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.
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
#include<stdio.h>
#define MAX 5000
typedef struct _edge
{
int x,y;
int weight;
};
struct _edge edge[MAX];
int father[MAX];
int rank[MAX];
int sum;
void initial()
{
memset(rank,0,sizeof(rank));
int _i;
for(_i=0;_i<MAX;_i++)
father[_i]=_i;
sum=0;
}
int Find_Set(int x)
{
if(x!=father[x])
father[x]=Find_Set(father[x]);
return father[x];
}
int Union_Set(int x,int y,int w)
{
int a,b;
a=Find_Set(x);
b=Find_Set(y);
if(a==b) return 0;
if(rank[x]>rank[y])
father[b]=a;
else
{
father[a]=b;
if(rank[x]==rank[y]) rank[x]++;
}
sum+=w;
return 1;
}
int cmp(const void *a,const void *b)
{
return (*(struct _edge*)a).weight>(*(struct _edge*)b).weight?1:-1;
}
int main()
{
int n;
int i,j;
int cnt;
int tmp;
int x,y;
while(scanf("%d",&n)!=EOF)
{
cnt=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i<j)
{
edge[cnt].x=i;
edge[cnt].y=j;
scanf("%d",&edge[cnt].weight);
cnt++;
}
else scanf("%d",&tmp);
}
qsort(edge,cnt,sizeof(edge[0]),cmp);
initial();
for(i=0;i<cnt;i++)
{
x=Find_Set(edge[i].x);
y=Find_Set(edge[i].y);
if(x!=y) Union_Set(x,y,edge[i].weight);
}
printf("%d/n",sum);
}
return 0;
}
本文介绍了一种通过最小生成树算法解决农业区域光纤网络铺设问题的方法。为了将互联网接入所有农场,市长Farmer John需要计算连接所有农场所需的最短光纤长度。文章提供了一个具体的示例输入和输出,并给出了完整的C语言实现代码。
404

被折叠的 条评论
为什么被折叠?



