poj1287-Kruskal和prim算法。
来源:互联网 发布:黑客常用的编程语言 编辑:程序博客网 时间:2024/06/06 02:19
学了这两种算法,总觉得还没完全体会,体会两种方法的不同,我个人喜爱用Kruskal算法,因为它不像prim算法那样还要初始化,for语句也要小一些,看起来比prim算法更整洁。
下面以poj1287为列:
Description
You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.
Input
The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.
Output
For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.
Sample Input
1 02 31 2 372 1 171 2 683 71 2 192 3 113 1 71 3 52 3 893 1 911 2 325 71 2 52 3 72 4 84 5 113 5 101 5 64 2 120
Sample Output
0171626
prim算法
#include<stdio.h>#include<string.h>
const int INF = 0x3f3f3f3f;int min(int x, int y) { return x < y ? x : y;}int main(){ int i,j,n,r,p,t,k,sum=0; int a[51][51],w[3000]; while(scanf("%d",&p),p) { scanf("%d",&n); memset(a,0x3f,sizeof(a)); memset(w,0x3f,sizeof(w)); for(i=1;i<=n;i++) { scanf("%d%d",&r,&t); scanf("%d",&a[r][t]); a[t][r] = a[r][t] = min(a[t][r], a[r][t]); } for(i=1;i<=p;i++) w[i]=a[1][i]; w[1] = 0; for(i=1;i<p;i++) { int min=INF; for(j=1;j<=p;j++) if(w[j]<min && w[j]) {k=j;min=w[j];} w[k]=0,sum+=min; for(j=1;j<=p;j++) if(w[j]>a[k][j]) w[j]=a[k][j]; } printf("%d\n",sum); sum=0; } return 0;}/* 4 61 2 32 3 31 3 22 4 53 4 12 1 2 *//*4 71 2 52 1 22 1 31 3 42 3 53 4 12 4 3*//*6 101 3 52 3 43 4 12 1 55 2 43 5 36 2 74 2 53 5 56 1 2*/Kruskal算法
#include<cstdio>#include<algorithm>using namespace std;struct node{ int x,y,c; }edge[3000];int f[55],n,p;bool cmp(node a,node b){ if(a.c<b.c) return 1; return 0;}int find(int x){ return x==f[x]?x:find(f[x]);}void krusal(){ int i,sum=0; for(i=1;i<=p;i++) f[i]=i; for(i=0;i<n;i++) { int x=find(edge[i].x); int y=find(edge[i].y); if(x!=y) { f[x]=y; sum+=edge[i].c; } } printf("%d\n",sum);}int main(){ while(scanf("%d",&p),p) { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].c); sort(edge,edge+n,cmp); krusal(); } return 0;}
- poj1287-Kruskal和prim算法。
- POJ1287 Networking(最小生成树,Kruskal,Prim)
- Prim算法和Kruskal算法
- Prim算法和Kruskal算法
- Prim算法和Kruskal算法
- Prim算法和Kruskal算法
- Prim算法和Kruskal算法
- Prim算法和Kruskal算法
- Prim算法和Kruskal算法
- Prim算法和Kruskal算法
- Prim算法和Kruskal算法
- Prim算法和Kruskal算法
- prim 算法和 kruskal算法
- Prim算法和Kruskal算法
- Prim算法和Kruskal算法
- poj1287 Networking 最小生成树模板题。prim+kruskal算法AC
- poj1287 kruskal
- [附图]Kruskal算法和Prim算法
- Windows API函数大全(1)
- NtContinue笔记
- 在Unicode版Inno Setup中使用ISSkin给安装程序添加皮肤
- 百度音乐接口
- 地图碎片生成
- poj1287-Kruskal和prim算法。
- (两篇)深入理解C++的动态绑定和静态绑定
- 设备驱动的ioctl函数
- linux下的string库函数
- SQL SERVER 2008 阻止保存要求重新创建表的更改
- [笔试题] Google笔试题整理(超全!)附部分答案
- 深入理解Aireplay-ng各种攻击及其原理
- 帝国cms如何实现当前信息页高亮显示
- 棋牌游戏,赋闲老人的另一位“玩伴”