POJ 1258 Agri-Net(Prim求最小生成树)
Agri-Net
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 Source |
[Submit] [Go Back] [Status] [Discuss]
最小生成树——Minimum Spanning Tree,是图论中比较重要的模型,通常用于解决实际生活中的路径代价最小一类的问题。我们首先用通俗的语言解释它的定义:
对于有n个节点的有权无向连通图,寻找n-1条边,恰好将这n个节点相连,并且这n-1条边的权值之和最小。
对于MST问题,通常常见的解法有两种:Prim算法 或者 Kruskal算法+并查集
对于最小生成树,一定要注意其定义是在无向连通图的基础上,如果在有向图中,那么就需要另外的分析,单纯用无向图中的方法是不能得出正确解的,这一点我在比赛中确实吃过亏
好了,进入正题:
Prim算法:(基于点的贪心思路)由于是基于点的算法,因此适合于稠密图,一下给出代码没有经过堆优化,时间复杂度为O(N^2)
记原图为G,生成树图为MST,其中G的节点个数为n个
算法描述如下:
- 任取G中的一点,加入MST中——这一步的作用是选择一个节点作为整个算法的起点
- 采用贪心策略,将刚刚加入的节点记为u,以u为中心,检查与u相连且没有加入MST的节点(未访问过的节点),选择权值最小的边,如果有多条边的权值均最小,则任取一条边。——贪心策略,选择局部最优
- 将所选择的边中,不在MST中的那个节点,加入MST——举例来说,比如(u,v)是当前与u相连,v不再MST中,且权值最小的边,则边(u,v)被选中,并将v加入MST。
- 如果步骤2-3被执行了n-1次,则退出,反之则返回到步骤2。——由于Prim算法初始化时加入了起点,而步骤2-3每执行一次都会加入一个新的节点,所以只需判断执行次数。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
//inf为路径权上界,maxn为图的临接矩阵的点数
//vis是记录是否访问过,cost[i]记录到达第i个节点的最小代价
const int inf=0x7fffffff,maxn=101;
int G[maxn][maxn],vis[maxn],cost[maxn],n;
//len为MST长度
int prim(){
memset(vis,0,sizeof(vis));
//加入起始节点
int pos=1,min=inf,len=0,cnt=n;
vis[1]=1;
for(int v=2;v<=n;v++)cost[v]=G[pos][v];
//加入剩余n-1个节点
while(--cnt){
for(int i=1;i<=n;i++)if(!vis[i]&&cost[i]<min){
pos=i;min=cost[i];
}
len+=min;vis[pos]=1;
//以新加入的节点为中心,更新权值信息
for(int i=1;i<=n;i++)if(!vis[i]&&G[pos][i]<cost[i])
cost[i]=G[pos][i];
min=inf;
}
return len;
}
int main()
{
//freopen("in.txt","r",stdin);
//int n;
while(cin>>n&&n)
{
memset(cost,0,sizeof(cost));
memset(G,0,sizeof(G));
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>G[i][j];
}
}
cout<<prim()<<endl;
}
}