最小生成树Prim算法 Highways POJ
来源:互联网 发布:python 监控日志文件 编辑:程序博客网 时间:2024/05/01 02:38
Description
Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.
The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length.
Input
The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of ith town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.
The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway.
Output
If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.
Sample Input
91 50 0 3 24 55 10 45 21 25 331 39 71 2
Sample Output
1 63 74 95 78 3题意:现在平面上有n个点的坐标。给出m条边,让你继续连接两个点使所有点连起来,使得新连的边长度最短,输出你连接的两点(输出顺序可以乱)
MST(Minimum Spanning Tree,最小生成树)问题有两种通用的解法,Prim算法就是其中之一,它是从点的方面考虑构建一颗MST,大致思想是:设图G顶点集合为U,首先任意选择图G中的一点作为起始点a,将该点加入集合V,再从集合U-V中找到另一点b使得点b到V中任意一点的权值最小,此时将b点也加入集合V;以此类推,现在的集合V={a,b},再从集合U-V中找到另一点c使得点c到V中任意一点的权值最小,此时将c点加入集合V,直至所有顶点全部被加入V,此时就构建出了一颗MST。因为有N个顶点,所以该MST就有N-1条边,每一次向集合V中加入一个点,就意味着找到一条MST的边。
用图示和代码说明:
初始状态:
设置2个数据结构:
lowcost[i]:表示以i为终点的边的最小权值,当lowcost[i]=0说明以i为终点的边的最小权值=0,也就是表示i点加入了MST
mst[i]:表示对应lowcost[i]的起点,即说明边<mst[i],i>是MST的一条边,当mst[i]=0表示起点i加入MST
我们假设V1是起始点,进行初始化(*代表无限大,即无通路):
lowcost[2]=6,lowcost[3]=1,lowcost[4]=5,lowcost[5]=*,lowcost[6]=*
mst[2]=1,mst[3]=1,mst[4]=1,mst[5]=1,mst[6]=1,(所有点默认起点是V1)
明显看出,以V3为终点的边的权值最小=1,所以边<mst[3],3>=1加入MST
此时,因为点V3的加入,需要更新lowcost数组和mst数组:
lowcost[2]=5,lowcost[3]=0,lowcost[4]=5,lowcost[5]=6,lowcost[6]=4
mst[2]=3,mst[3]=0,mst[4]=1,mst[5]=3,mst[6]=3
明显看出,以V6为终点的边的权值最小=4,所以边<mst[6],6>=4加入MST
此时,因为点V6的加入,需要更新lowcost数组和mst数组:
lowcost[2]=5,lowcost[3]=0,lowcost[4]=2,lowcost[5]=6,lowcost[6]=0
mst[2]=3,mst[3]=0,mst[4]=6,mst[5]=3,mst[6]=0
明显看出,以V4为终点的边的权值最小=2,所以边<mst[4],4>=4加入MST
此时,因为点V4的加入,需要更新lowcost数组和mst数组:
lowcost[2]=5,lowcost[3]=0,lowcost[4]=0,lowcost[5]=6,lowcost[6]=0
mst[2]=3,mst[3]=0,mst[4]=0,mst[5]=3,mst[6]=0
明显看出,以V2为终点的边的权值最小=5,所以边<mst[2],2>=5加入MST
此时,因为点V2的加入,需要更新lowcost数组和mst数组:
lowcost[2]=0,lowcost[3]=0,lowcost[4]=0,lowcost[5]=3,lowcost[6]=0
mst[2]=0,mst[3]=0,mst[4]=0,mst[5]=2,mst[6]=0
很明显,以V5为终点的边的权值最小=3,所以边<mst[5],5>=3加入MST
lowcost[2]=0,lowcost[3]=0,lowcost[4]=0,lowcost[5]=0,lowcost[6]=0
mst[2]=0,mst[3]=0,mst[4]=0,mst[5]=0,mst[6]=0
至此,MST构建成功,如图所示:
建图:根据坐标建立无向图,权值设为距离的平方即可,这样可以避免sqrt后权值变为double型,避免精度损失。对于已联通的两点,更新这两点的权值为0即可。
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<math.h>using namespace std;//普利姆,注意建图技巧const int maxn=751;const int INF=0x3f3f3f3f;int map[maxn][maxn];//两点间距离int dis[maxn];//dis[i]表示i点(未连接的点)到(已连接的点)的最短距离int vis[maxn];//标记,1为已选过的点int Edge[maxn];//Edge[i]的值为k,表示i到k是一条生成树内的边struct node{ int x; int y;} Point[maxn]; //第i个点的坐标int N;//点的数量int M;//更新边的数量void init(){ scanf("%d",&N); for(int i=1; i<=N; i++)//建图 { scanf("%d%d",&Point[i].x,&Point[i].y); for(int j=1; j<i; j++)//为什么这里不取sqrt,因为完全没必要 map[i][j]=map[j][i]=(Point[i].x-Point[j].x)*(Point[i].x-Point[j].x)+(Point[i].y-Point[j].y)*(Point[i].y-Point[j].y); map[i][i]=INF;//自己不可能到自己 } scanf("%d",&M); int x,y; while(M--)//更新图 { scanf("%d%d",&x,&y); map[x][y]=map[y][x]=0; } memset(vis,0,sizeof(vis)); vis[1]=1; for(int i=1; i<=N; i++) { dis[i]=map[i][1]; Edge[i]=1;//初始化为存储i到1的边 }}void Prim(){ for(int i=1; i<N; i++) { int minn=INF; int point_minn; for(int j=1; j<=N; j++) if(vis[j]==0&&minn>dis[j]) { minn=dis[j]; point_minn=j; } //选出最优点; vis[point_minn]=1; for(int k=1; k<=N; k++)//用最优点更新其他点。 if(vis[k]==0&&dis[k]>map[point_minn][k]) { Edge[k]=point_minn;//这里是输出方式的技巧 dis[k]=map[point_minn][k]; } if(map[Edge[point_minn]][point_minn])//判断是否为已知的m条边 printf("%d %d\n",Edge[point_minn],point_minn); }}int main(){ init(); Prim(); return 0;}
- 最小生成树Prim算法 Highways POJ
- POJ 2485-Highways 最小生成树Prim算法
- POJ 2485 Highways 图论 prim算法 最小生成树
- POJ 2485 Highways 最小生成树 prim算法
- POJ 2485 Highways(最小生成树prim算法)
- poj 1751 Highways ( 最小生成树--prim算法 )
- poj 2485 Highways prim最小生成树
- POJ 2485 Highways (prim最小生成树)
- POJ 1751 Highways(最小生成树prim)
- poj 1751 Highways 最小生成树 prim
- POJ 2485 Highways 【最小生成树Prim】
- POJ 2485 Highways(prim 最小生成树)
- POJ 2485 Highways(最小生成树 Prim)
- poj 2485 Highways(kruscal or prim最小生成树)
- POJ 2485:Highways:典型prim最小生成树(2)
- poj 2485 Highways(prim求最小生成树)
- POJ 2485:Highways(最小生成树&&prim)
- poj 2485 Highways(最小生成树,prim)
- 【正则表达式图片总结】+常用的表单验证代码
- 快速排序
- Hibernate3的动态条件查询DetachedCriteria
- 给你一个App,你将如何测试?
- 使用报表开发工具Ireport生成Pdf报表的总结
- 最小生成树Prim算法 Highways POJ
- C语言中const位置与应用辨析
- [转]JSTL
- table总结insertRow、deleteRow
- 四十年前的 6502 CPU 指令翻译成 JS 代码会是怎样
- 图像轮廓提取知识总结
- 强烈推荐 Android Studio 插件
- [基础]由复选框实现的批量删除
- 使用Table的insertRow实现某一模块的复制