我是靠谱客的博主 高大白猫,最近开发中收集的这篇文章主要介绍【最小生成树之prim算法】POJ-1258---Agri-Net,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


Agri-Net
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15966 Accepted: 6494

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

假设V是图中顶点的集合,E是图中边的集合,TE为最小生成树中的边的集合,则prim算法通过以下步骤可以得到最小生成树:

  1:初始化:U={u 0},TE={}。此步骤设立一个只有结点u 0的结点集U和一个空的边集TE作为最小生成树的初始行态,在随后的算法执行中,这个行态会不断的发生变化,直到得到最小生成树为止。

  2:在所有u∈U,v∈V-U的边(u,v)∈E中,找一条权最小的边(u 0,v 0),将此边加进集合TE中,并将此边的非U中顶点加入U中。此步骤的功能是在边集E中找一条边,要求这条边满足以下条件:首先边的两个顶点要分别在顶点集合U和V-U中,其次边的权要最小。找到这条边以后,把这条边放到边集TE中,并把这条边上不在U中的那个顶点加入到U中。这一步骤在算法中应执行多次,每执行一次,集合TE和U都将发生变化,分别增加一条边和一个顶点,因此,TE和U是两个动态的集合,这一点在理解算法时要密切注意。

  3:如果U=V,则算法结束;否则重复步骤2。可以把本步骤看成循环终止条件。我们可以算出当U=V时,步骤2共执行了n-1次(设n为图中顶点的数目),TE中也增加了n-1条边,这n-1条边就是需要求出的最小生成树的边。

代码在此:
#include<stdio.h>
int map[101][101],low[101],flag[101];
int prim(int m)
{
int i,j,k,l,num=0,time=1;
flag[0]=0;
for(i=1;i<m;i++)
{
low[i]=map[0][i];
flag[i]=1;
}
while(time<m)
{
j=0; int min=10000;
for(i=1;i<m;i++)
{
if(low[i]<min&&flag[i]==1)
{
min=low[i];
j=i;
}
}
flag[j]=0;
num+=min;
for(i=1;i<m;i++)
{
if(map[j][i]<low[i]&&flag[i]==1)
{
low[i]=map[j][i];
}
}
time++;
}
return num;
}
int main()
{
int m,i,j;
while(~scanf("%d",&m))
{
for(i=0;i<m;i++)
for(j=0;j<m;j++)
scanf("%d",&map[i][j]);
printf("%dn",prim(m));
}
return 0;
}

最后

以上就是高大白猫为你收集整理的【最小生成树之prim算法】POJ-1258---Agri-Net的全部内容,希望文章能够帮你解决【最小生成树之prim算法】POJ-1258---Agri-Net所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(29)

评论列表共有 0 条评论

立即
投稿
返回
顶部