POJ-1258 Agri-Net(最小生成树+Prim算法)
Agri-Net
Time Limit: 1000MS | Memory Limit: 10000K | |
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
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAXN=110;
struct Node
{
int adjvex;
int lowcost;
}closedge[MAXN];
int cost[MAXN][MAXN];
int MinV_Ucost(int n) //这个函数是用来找V集合的点到U集合点的最短cost(当然是只有两个点)
{
int MINN=0x3f3f3f3f;
int i,v;
for(i=1;i<=n;i++)
{
if(closedge[i].lowcost!=0) //等于0的就已经进U集合了,不做考虑,这样避免了生成环。
{
if(closedge[i].lowcost<MINN)
{
MINN=closedge[i].lowcost;
v=i;
}
}
}
if(MINN==0x3f3f3f3f)
{
printf("不连通\n"); //原图不连通,至少有一个不为INF才连通啊
return 0;
}
else
return v;
}
int Prim(int u,int n) //u是U集合的第一个点,可以认为假设
{
int i,j;
int v;
int ans=0;
closedge[u].lowcost=0; //=0就是将该结点放入U集合中
for(i=1;i<=n;i++)
{
if(i!=u)
{
closedge[i].adjvex=u; //i是在V集合中的点,同时说明第一个点是没有adjvex的
closedge[i].lowcost=cost[u][i];
}
}
for(j=1;j<n;j++) //第一个点已经确定,进入U,还剩n-1个点没进U集合
{
v=MinV_Ucost(n);
ans+=closedge[v].lowcost;
/*u=closedge[v].adjvex; //这样就多了个可以输出两邻接点的功能
printf("%d %d\n",u,v);*/
closedge[v].lowcost=0; //把新点加入U集合。
for(i=1;i<=n;i++) //更新V各点到U各点的最短距离。
{
if(cost[v][i]<closedge[i].lowcost) //在U中的点是0,所以if语句无视
{
closedge[i].lowcost=cost[v][i];
closedge[i].adjvex=v;
}
}
}
return ans;
}
int main()
{
int n;
int i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
printf("%d\n",Prim(1,n));
}
return 0;
}