题意描述:
高速路修路问题,有一些点是可以直接连通的现在需要修其他的路径不过现在要的不是总的路径长度,而是要修的每一条路的顶点编号
原题:
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can’t reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system.

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 input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built.

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 i th 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
Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by a space.

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

9
1 5
0 0
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2

Sample Output

1 6
3 7
4 9
5 7
8 3

解题思路:
重新定义一个数组p来表示更新节点的父亲节点例如:
这样一组数据:
1 2 1
1 3 2
2 4 6
3 4 2
在最开始的时候都把父亲节点定为1,(下面就是普利姆算法)然后找距离1最近的点然后找到了节点2然后就该输出这个点和他的父亲节点;-----原因:(具体分析看代码中的讲述)
先看这一部分代码:

            min=inf;for(i=1; i<=n; i++){if(book[i]==0&&dis[i]<min){min=dis[i]; j=i;}}book[j]=1;//在这个地方找到的已经是当前距离根节点最近的点也就是说这条路是一定要修的//可以想一下,以前我们求距离的时候在这个地方把他们的距离加上了这一次我们//同样根据题目要求的不同吧要求的距离换成了两节点的信息(应该可以懂了吧)if(e[p[j]][j]!=0)printf("%d %d\n", p[j],j);

开始寻找2的出边找到了4,当时book[4]==0又因为dis[4]当时的数值是无穷大所以就更新为6,也就是下面的操作

             for(k=1; k<=n; k++){if(book[k]==0&&dis[k]>e[j][k]){dis[k]=e[j][k];p[k]=j;//找到要更新的节点是k节点//上一步找到距离源节点最近的点是j//然后再通过j节点并且作比较的也是e[j][k](代表j到//k的距离也就是说j和k是连接的)来更新dis[k]的,//现在应该理解当前k的父亲节点就是j}}

每次对于更新的点不能保证更新的最近的节点是dis[k],所以我们是在上一步直到j节点更新为最近节点的时候才把它和它的父亲节点输出(说实话做这道题的时候想了很具就是想不明白父亲节点的更换甚至当时还想到用结构体来记录父亲节点但是显然失败了因为出现的距离可能相等)
下面是AC代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
int e[2000][2000];
int dis[20000],p[2000];
int book[20000];
int inf=99999999;
struct node{int x;int y;
}s[2000];
int main()
{int n,m,t,min,i,j,k,a,b,w,r;while(scanf("%d", &n)!=EOF){for(i=1; i<=n; i++)scanf("%d %d", &s[i].x,&s[i].y);for(i=1; i<=n; i++){for(j=1; j<=n; j++){w=s[i].x-s[j].x;r=s[i].y-s[j].y;e[i][j]=e[j][i]=w*w+r*r;}} scanf("%d", &m);for(i=1; i<=m; i++){scanf("%d %d", &a,&b);e[a][b]=e[b][a]=0;}for(i=1; i<=n; i++)p[i]=1;for(i=1; i<=n; i++)dis[i]=e[1][i];memset(book,0,sizeof(book));book[1]=1;for(t=1; t<n; t++){min=inf;for(i=1; i<=n; i++){if(book[i]==0&&dis[i]<min){min=dis[i]; j=i;}}book[j]=1;if(e[p[j]][j]!=0)printf("%d %d\n", p[j],j);for(k=1; k<=n; k++){if(book[k]==0&&dis[k]>e[j][k]){dis[k]=e[j][k];p[k]=j;}}}}return 0;}

最小生成树——Highways(普利姆算法结合对于父亲节点标记)相关推荐

  1. (王道408考研数据结构)第六章图-第四节1:最小生成树之普利姆算法(思想、代码、演示、答题规范)

    文章目录 一:普利姆(Prim)算法算法思想 二:普利姆(Prim)算法注意点 三:普利姆(Prim)算法代码实现 四:普利姆(Prim)算法代码视频演示 五:普利姆(Prim)算法动画演示 六:普利 ...

  2. 最小生成树(普利姆算法、克鲁斯卡尔算法)

    给定一个带权的无向连通图,如何选取一棵生成树,使树上所有边上权的总和为最小,这叫最小生成树. 求最小生成树的算法 (1) 克鲁斯卡尔算法 图的存贮结构采用边集数组,且权值相等的边在数组中排列次序可以是 ...

  3. [算法]最小生成树-普利姆算法

    2019独角兽企业重金招聘Python工程师标准>>> 目前正在看<大话数据结构>,其中介绍了普利姆算法,自己对算法理解能力太差,能够手写求出最小生成树,但是写出算法代码 ...

  4. 普利姆算法和克鲁斯卡尔算法求解最小生成树

    Q:最小生成树有什么用? A:譬如我要去五个城市旅游,每两个城市之间可能有路也可能没有,路的距离可能一样也可能不一样,随机从一个城市出发,我想要把每个城市走一遍,怎么样走过的路距离最短,比如我想从上海 ...

  5. 最小生成树算法普利姆算法和克鲁斯卡尔算法实现

    最小生成树算法: 普里姆算法:顶点集合N,辅助顶点集合S,初始化中,将出发点vi加入S,并从N中删除 1.从顶点集合N中找到一条到集合S最近的边(vi,vj),存储该边,并将vj从N移到S中 2.重复 ...

  6. 普利姆算法(prim)求最小生成树(MST)过程详解

    生活中最小生成树的应用十分广泛,比如:要连通n个城市需要n-1条边线路,那么怎么样建设才能使工程造价最小呢?可以把线路的造价看成权值求这几个城市的连通图的最小生成树.求最小造价的过程也就转化成求最小生 ...

  7. 【数据结构】图的应用(普利姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法、拓扑排序)

    最小生成树 什么是最小生成树 是一棵树 - 无回路 - |V|个顶点一定有|V|-1条边 是生成树 - 包含全部顶点 - |V|-1条边全在图里 贪心算法 什么是"贪":每一步都要 ...

  8. 普利姆算法解决最短修路问题

    普利姆算法 1.应用场景-修路问题 2.最小生成树 3.普利姆算法介绍 4.普利姆算法的最为简单的理解(重点): 理论上7个点要6条路就可以连通,随便从一个结点(村庄)出发(假设为A),先找该结点和邻 ...

  9. 普利姆算法和修路问题

    修路问题 看一个应用场景和问题: 有胜利乡有7个村庄(A, B, C, D, E, F, G) ,现在需要修路把7个村庄连通 各个村庄的距离用边线表示(权) ,比如 A – B 距离 5公里 问:如何 ...

最新文章

  1. 掉坑里了,小记一下.
  2. 纯CSS3画出小黄人并实现动画效果
  3. 车牌识别python实现ubuntu_python利用百度云接口实现车牌识别
  4. 莫凡机器学习课程笔记
  5. 【蓝桥杯官网训练 - 历届试题】对局匹配(dp,思维,取模)
  6. 如何使用git管理crontab任务
  7. java int biginteger_[十六]基础类型BigInteger简介
  8. 计算机技术在工程施工中的应用,浅析计算机技术及网络在工程施工中的应用
  9. 如何评估 Serverless 服务能力?这份报告给出了 40 条标准
  10. 专访三星高级工程师 Joonyong Park:Adaptive Performance 让游戏的高性能和高画质二者兼得
  11. python mysql树_python操作mysql数据库
  12. centos5.5 下面 lnmp环境遇到的小问题
  13. 我如何学习:开篇 先提升下肾上腺素
  14. 惠普打印机驱动服务器系统,在打印机服务器(系统WIN2003)上安装了HP5100 打印机,客户机系统WIN7 64位,现没法添加HP5100的驱动...
  15. 简单好用的开源会议室预约系统
  16. html5分辨率异常自动检测
  17. QT 主线程子线程互相传值
  18. 工业物联网平台的效益有哪些
  19. 径向基网络(RBF network)之BP监督训练
  20. 基于lamp搭建Discuz论坛

热门文章

  1. Android onKeyDown自定义功能
  2. ECMA及ecma5下新增的数组
  3. Mactype - 让 Windows 字体更漂亮
  4. kubernetes二进宫系列——Kubernetes TLS BootStrapping流程引导分析
  5. 我把毕业设计命名成“垃圾”发给了导师...
  6. 强网杯线上赛一道套娃MISC题解
  7. 【再论深度学习必死】马库斯回应14大质疑,重申深度学习怀疑论
  8. C#-深入理解async和await的作用及各种适用场景和用法
  9. [转]xbeta(善用佳软) 【xbeta精心荐精品之免费软件列表 】牛!
  10. 存量市场下,雅迪的高端化之路举步维艰?