码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构之 普利姆算法总结

时间:2014-07-31 12:15:26      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   strong   io   

                                                                         Agri-Net

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status

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


普里姆算法的分析:
从某一个点出发,找到与这个点相连的最小的边,加进来。把该边的另一个点包括进来,再从这两个点出发,找与之相连的最小的边。
继续加进来,继续这样做下去,直到找到n-1条边。

取图中任意一个顶点 u ∈ U作为生成树的根,之后往生成树上添加新的顶点 v ∈ V-U。

在添加的顶点 v和已经在生成树上的顶点u 之间必定存在一条边(u,v),并且该边的权值在所有连通顶点集U 和 V-U 之间的边中取值最小。

之后继续往生成树上添加顶点,直至生成树上U含有 n 个顶点为止。


普利姆算法的代码:

#include <stdio.h>
#include <string.h>

int map[110][110];
int vt[110];
int dis[110]; //从已知到未知点集路径权值 记录
int sum;

void prim(int n)
{
	int i, j;
	int pos;
	memset(vt, 0, sizeof(vt ));
	memset(dis, 0, sizeof(dis ));

	for(i=1; i<=n; i++)
	{
		dis[i] = map[1][i] ;
	}

	vt[1] = 1;
    int min;
	
	for(i=1; i<n; i++)
	{
		min = 10000000;
		for(j=1; j<=n; j++)
		{
			if(vt[j]==0 && dis[j] < min )
			{
				min = dis[j] ;
				pos = j;
			}
		}
		sum = sum + min ; //权值被加入进来
		
		vt[pos] = 1; //标记被访问
		for(j=1; j<=n; j++)  //更新权值数组 (这个步骤最好自己跑一下,否则不好理解 )
		{
			if(vt[j]==0 && map[pos][j] < dis[j] )
			{
				dis[j] = map[pos][j] ;
			}
		}
	}
	printf("%d\n", sum );
}

int main()
{
	int n, dd;
	int i, j;
	
    while(scanf("%d", &n)!=EOF)
	{
		//memset(vt, 0, sizeof(vt ));
	//	memset(dis, 0, sizeof(dis ));
		sum = 0;
		 for( i=1;i<=n;i++)
		{
            for( j=1;j<=n;j++)  
            {  
                map[i][j]=10000000;  
            }  
		}
        for(i=1;i<=n;i++)  
            map[i][i]=0;  
        
        for(i=1; i<=n; i++)  
        {  
           for(j=1; j<=n; j++)
		   {
			   scanf("%d", &dd);
			   if(i!=j)
			   {
				   if(map[i][j] > dd)
				   {
				    map[i][j]=dd;
					map[j][i]=dd;
				   }
			   }
		   }  
        }  
		prim(n);
    }
	return 0;
}

 


数据结构之 普利姆算法总结,布布扣,bubuko.com

数据结构之 普利姆算法总结

标签:des   style   blog   http   color   os   strong   io   

原文地址:http://www.cnblogs.com/yspworld/p/3879979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!