首页 > 代码库 > 数据结构之 普利姆算法总结
数据结构之 普利姆算法总结
Agri-Net
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
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
40 4 9 214 0 8 179 8 0 1621 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;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。