作者在 2015-10-12 16:05:25 发布以下内容
Agri-Net
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 46162 | Accepted: 18988 |
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
Source
johu当上了市长,他其中一个当选政策就是让所有的农场都连上互联网
他想用最小长度的光纤去把所有的农场连接起来农场数N 3 100
n*n 该农场到其他农场的距离
求把所有农场两两可达需要的最短网线长度
思路:
prim就行。
最小生成树prim算法:
设V为所有顶点集合,首先选择任意一个顶点a放进U集合中,然后
在V-U中找一个顶点b,b要满足以下条件:b到U中所有的顶点长度最小。
迭代循环。
#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;
#define maxn 105
#define inf INT_MAX
int sum;
int g[maxn][maxn];
int n;
int prim()
{
sum=0;
int form[maxn];
int dis[maxn];
for(int i=2;i<=n;i++)
{
dis[i]=g[1][i];
form[i]=1;
}
form[1]=0;
for(int i=2;i<=n;i++)
{
int min=inf;
int u=0;
for(int j=2;j<=n;j++)
{
if(dis[j]!=0&&dis[j]<min)
{
min=dis[j];
u=j;
}
}
dis[u]=0;
sum+=min;
dis[u]=0;
for(int j=2;j<=n;j++)
{
if(g[u][j]<dis[j])
{
dis[j]=g[u][j];
form[j]=u;
}
}
}
return sum;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(g,inf,sizeof(g));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&g[i][j]);
g[j][i]=g[i][j];
}
}
printf("%d\n",prim());
}
}