博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1751——Highways(部分确定的最小生成树)
阅读量:2343 次
发布时间:2019-05-10

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

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 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

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

n个城市之间已经有一些路连接,求在此情况之下的最小生成树。

还是将已建成的路设为0,这样就能加入最小生成树,同时用pre记录每条路径方便枚举输出

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 2005#define Mod 10001using namespace std;struct Node{ double x,y;};Node p[MAXN];int n,vis[MAXN],ans,pre[MAXN];double map[MAXN][MAXN],dis[MAXN];double getdis(int a,int b){ double x=p[a].x-p[b].x,y=p[a].y-p[b].y; return sqrt(x*x+y*y);}double m[MAXN];void prim(){ int i,j,pos; double min; ans=0; memset(vis,0,sizeof(vis)); vis[1]=1,pos=1; for(i=1; i<=n; ++i) { dis[i]=map[pos][i]; pre[i]=1; } for(i=1; i
map[pos][j]) { dis[j]=map[pos][j]; pre[j]=pos; } } //return ans;}int main(){ while(~scanf("%d",&n)) { for(int i=1; i<=n; ++i) scanf("%lf%lf",&p[i].x,&p[i].y); for(int i=1; i<=n; ++i) for(int j=1; j<=n; ++j) map[i][j]=getdis(i,j); int m,a,b; scanf("%d",&m); for(int i=1; i<=m; ++i) { scanf("%d%d",&a,&b); map[a][b]=0; map[b][a]=0; } prim(); } return 0;}

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

你可能感兴趣的文章
如何利用ROS MoveIt快速搭建机器人运动规划平台?
查看>>
catkin_make &amp;amp;catkin build
查看>>
Camera和IMU的标定过程之kalibr 源码编译
查看>>
在ubuntu下安装python的numpy和scipy模块
查看>>
Ubuntu下apt-get与pip安装命令的区别
查看>>
linux CMakeLists.txt 语法
查看>>
cmake 简介
查看>>
CMake学习笔记(1)——用CMake编译一个hello world程序
查看>>
cmake使用总结---工程主目录CMakeList文件编写
查看>>
CMake学习之路
查看>>
cmake学习笔记6-catkin的CmakeList.txt讲解
查看>>
cmake手册详解
查看>>
Maplab框架介绍(一)
查看>>
Maplab开源VI-SLAM框架介绍
查看>>
maplab(1):安装
查看>>
陀螺仪随机误差的Allan方差分析
查看>>
Ubuntu 64位安装Adobe Reader 9.5.5
查看>>
Ubuntu 下如何查看已安装的软件
查看>>
Linux 系统下可以注释标注的pdf阅读器安装、比较和推荐
查看>>
福昕阅读器foxit reader Linux版
查看>>