Agri-Net
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 44549 Accepted: 18197
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
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 44549 Accepted: 18197
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
知道prime算法的模板,这道题就很简单了,每两个城市之间的距离都给出了,直接存进数组里,然后给V一个值,就是点的个数,然后进入prime函数里,返回的是最小生成树的长度。
/*
该prim算法对了,我没有给V赋值。
*/
#include <iostream>
#include <cstdio>
#define inf 1e7
using namespace std;
int cost[10005][1005];//表示边e=(u,v)的权值(不存在的情况下设为INF)
int mincost[10005];//从集合x出发的边到每个顶点的最小权值
bool used[10005];//顶点i是否包含在集合中
int V;//顶点数
int prim()
{
// cout<<"defesfsrgfs"<<endl;
for(int i=0;i<V;i++)
{
mincost[i]=inf;
used[i]=false;
}
mincost[0]=0;
int res=0;
while(true)
{
int v=-1;
//从不属于x的顶点中选取从x到其权值最小的顶点
for(int u=0;u<V;u++)
{
if(!used[u]&&(v==-1||mincost[u]<mincost[v]))
v=u;
}
if (v==-1) break;
used[v]=true;//把顶点v加入x
res+=mincost[v];//把边的长度加到结果里
for(int u=0;u<V;u++)
{
mincost[u]=min(mincost[u],cost[v][u]);
}
}
return res;
}
int main(void)
{
// freopen("E.txt","r",stdin);
int m;
while(scanf("%d%d",&V,&m)!=EOF&&(V||m))
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
int a;
scanf("%d",&a);
if(a)
cost[i][j]=a;
}
}
V=m;
// cout<<"fdfsdgfsd"<<endl;
printf("%d\n",prim());
}
return 0;
}