博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最小生成树 - H - Highways
阅读量:4126 次
发布时间:2019-05-25

本文共 4045 字,大约阅读时间需要 13 分钟。

Highways

Description

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

91 50 0 3 24 55 10 45 21 25 331 39 71 2

Sample Output

1 63 74 95 7
8 3
题意:给定N个地方的坐标,需要用路将其都连接起来,一些地方已经连接了起来,问还需要连接哪些路,打印路径。
题解:裸prim模板题,唯一注意在比较时需要用double类型,int会WA掉。
AC代码:
#include
#include
using namespace std;int n,m;const int maxn = 800;const int inf = 0x3f3f3f3f;double a[maxn][maxn];int x[maxn],y[maxn];int path[maxn];int vis[maxn];double length[maxn];double leng(double x1,double y1,double x2,double y2){ return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}void prim(){ for(int i=1;i<=n;i++) { length[i]=a[1][i]; vis[i]=0; path[i]=1; } for(int i=1;i<=n;i++) { double mi = inf; int u; for(int j=1;j<=n;j++) { if(vis[j]==0&&mi>length[j]) { mi=length[j]; u=j; } } vis[u]=1; for(int j=1;j<=n;j++) { if(vis[j]==0&&length[j]>a[u][j]) { length[j]=a[u][j]; path[j]=u; } } if(a[u][path[u]]==0)continue; else cout<
<<" "<
<
>n) { for(int i=1;i<=n;i++)cin>>x[i]>>y[i]; for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) a[i][j]=a[j][i]=leng(x[i],y[i],x[j],y[j]); cin>>m; for(int j=1;j<=m;j++) { int g,k; cin>>g>>k; a[g][k]=a[k][g]=0; } prim(); }}

转载地址:http://sbhpi.baihongyu.com/

你可能感兴趣的文章
Unix 命令,常用到的
查看>>
DLL中建立进程共享数据段需要注意的语法问题
查看>>
服务器端技术----Http请求的处理过程
查看>>
C语言-预处理指令2-条件编译
查看>>
C语言-预处理指令3-文件包含
查看>>
C语言-变量类型
查看>>
C语言-static和extern关键字1-对函数的作用
查看>>
C 语言-static和extern关键字2-对变量的作用
查看>>
【JavaScript 教程】浏览器—History 对象
查看>>
还不会正则表达式?看这篇!
查看>>
100道+ JavaScript 面试题,助你查漏补缺
查看>>
JavaScript深入理解之闭包
查看>>
这才是学习Vite2的正确姿势!
查看>>
7 个适用于所有前端开发人员的很棒API,你需要了解一下
查看>>
25个构建Web项目的HTML建议,你需要了解一下!
查看>>
【web素材】02-10款大气的购物商城网站模板
查看>>
6种方式实现JavaScript数组扁平化(flat)方法的总结
查看>>
如何实现a===1 && a===2 && a===3返回true?
查看>>
49个在工作中常用且容易遗忘的CSS样式清单整理
查看>>
20种在学习编程的同时也可以在线赚钱的方法
查看>>