| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 54239 | Accepted: 22524 |
Description
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
Output
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
//最小生成树;
#include<cstdio>
#include<iostream>
using namespace std;
int Map[510][510];
int lowcost[501];
const int inf=1<<22;
int main(){
int n;
while(cin>>n){
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&Map[i][j]);
int min_w;
int ans=0;
for(i=0;i<n;i++)
lowcost[i]=Map[0][i];
for(i=0;i<n-1;i++){
min_w=inf;
int point;
for(j=0;j<n;j++)
if(lowcost[j]&&lowcost[j]<min_w){
min_w=lowcost[j];
point=j;
}
lowcost[point]=0;
ans+=min_w;
for(j=0;j<n;j++)
if(lowcost[j]>Map[point][j])
lowcost[j]=Map[point][j];
}
cout<<ans<<endl;
}
}
构建农业网络
本文介绍了一个通过寻找最小生成树来解决农业区域内的网络连接问题的方法。市长Farmer John希望使用最少的光纤材料将所有农场连接起来,实现互联网覆盖。文章提供了一种算法实现方案,包括输入输出格式和样例。
4716

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



