N个点,给你N个点的坐标,现在还有Q条边已经连接好了。问把N个点怎么连接起来的花费的距离最短?
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.
 
Flatopia岛国完全平坦。不幸的是,Flatopia的公共高速公路系统非常糟糕。弗拉托利亚政府意识到了这个问题,并且已经建造了一些连接一些最重要城镇的高速公路。但是,仍有一些城镇无法通过高速公路抵达。有必要建造更多的高速公路,以便能够在不离开高速公路系统的情况下在任何一对城镇之间行驶。
Flatopian城镇的编号从1到N,城镇i的位置由笛卡尔坐标(xi,yi)给出。每条高速公路连接两个城镇。所有高速公路(原始高速公路和要建造的高速公路)都遵循直线,因此它们的长度等于城镇之间的笛卡尔距离。所有高速公路都可以在两个方向上使用。高速公路可以自由地相互交叉,但司机只能在位于两条高速公路尽头的小镇的高速公路之间切换。
Flatopian政府希望最大限度地降低建设新高速公路的成本。但是,他们希望保证每个城镇都可以从其他城镇到达公路。由于Flatopia是如此平坦,高速公路的成本总是与其长度成正比。因此,最便宜的高速公路系统将是最小化总公路长度的系统。

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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define inf 0x7fffffff
int dp[1010][1010],vis[1010],dis[1010],f[1010];
int x[1010],y[1010],m,n;
void djk()
{
int i,j;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
dis[i]=dp[1][i];//假设所有城镇都是与1号城镇连接最优;其实和dis数组一个意思,后面也一样更新
f[i]=1;
}
dis[1]=0;
vis[1]=1;
for(int i=1;i<=n;i++)
{
int k=0,minn=inf;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&minn>dis[j])
{
minn=dis[j];
k=j;
}
}
vis[k]=1;
if(dp[f[k]][k]!=0) //输出所有距离不为0相连的城镇即为需要建设的道路
printf("%d %d\n",k,f[k]);//不是已经建好的路就输出当前建立的边
for(int j=1;j<=n;j++)
{
if(!vis[j]&&dis[j]>dp[k][j])
{
dis[j]=dp[k][j];//当有更优的路线到v城镇更新距离,更新与v城镇相连的城镇号
f[j]=k;
}
}
}
}
int main()
{
while(~scanf("%d",&n))
{
int i,j,a,b;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);
for(int j=1;j<=i;j++)
dp[i][j]=dp[j][i]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
dp[a][b]=dp[b][a]=0;//城镇间已有公路距离为0
}
djk();
}
}
方法2:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct A
{
int a;
int b;
double c;
}q[1000010];
double cmp(struct A x,struct A y)
{
return x.c<y.c;
}
int f[10010],a[10010],b[10010];
int getf(int i)
{
if(f[i]==i)
return i;
else
{
f[i]=getf(f[i]);
return f[i];
}
}
int merge(int v,int u)
{
int t1,t2;
t1=getf(u);
t2=getf(v);
if(t1!=t2)
{
f[t1]=t2;
return 1;
}
return 0;
} int main()
{
int n,m,x,y,i,j,v,t,cut;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d%d",&a[i],&b[i]);
v=1;
for(i=1;i<n;i++)//存储路径
for(j=i+1;j<=n;j++)
{
q[v].a=i;
q[v].b=j;
q[v].c=(double)sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]));
v++;
}
v=v-1;
sort(q+1,q+v+1,cmp);
for(i=1;i<=n;i++)
f[i]=i;
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
merge(x,y);//查找是否为共同祖先,赋给共同祖先
}
cut=0;
for(i=1;i<=v;i++)
{
if(merge(q[i].a,q[i].b))//若不是一个共同祖先说明两城镇间未连接为需要建设的城镇
{ //存储两城镇
a[cut]=q[i].a;
b[cut]=q[i].b;
cut++;
}
if(cut==n-1)//这个判断只是为了提前结束循环,不加也能AC
break;
}
for(i=0;i<cut;i++)
printf("%d %d\n",a[i],b[i]);
return 0;
}


I - I(Highways)的更多相关文章

  1. H:Highways

    总时间限制: 1000ms 内存限制: 65536kB描述The island nation of Flatopia is perfectly flat. Unfortunately, Flatopi ...

  2. Highways(prim & MST)

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23421   Accepted: 10826 Descri ...

  3. poj2485 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  4. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

  5. poj 2485 Highways

    题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...

  6. POJ 1751 Highways (最小生成树)

    Highways Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  7. POJ 1751 Highways (最小生成树)

    Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...

  8. UVa 1393 (容斥原理、GCD) Highways

    题意: 给出一个n行m列的点阵,求共有多少条非水平非竖直线至少经过其中两点. 分析: 首先说紫书上的思路,编程较简单且容易理解.由于对称性,所以只统计“\”这种线型的,最后乘2即是答案. 枚举斜线包围 ...

  9. (poj) 1751 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...

  10. Highways poj 2485

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

随机推荐

  1. Linux设置系统时区

    https://www.xlsys.cn/1741.html 如果你的 Linux 系统时区配置不正确,必需要手动调整到正确的当地时区.NTP 对时间的同步处理只计算当地时间与 UTC 时间的偏移量, ...

  2. 白日梦的Elasticsearch笔记(一)基础篇

    目录 一.导读 1.1.认识ES 1.2.安装.启动ES.Kibana.IK分词器 二.核心概念 2.1.Near Realtime (NRT) 2.2.Cluster 2.3.Node 2.4.In ...

  3. PAT天梯赛练习 L3-003 社交集群 (30分) DFS搜索

    题目分析: 一共有N个编号为1~1000的人,以及一共有编号为1~1000种不同的兴趣,在题目给出1~N编号的人员每个人喜欢的兴趣的id后,要求统计出不同的人员集合的个数以及每个人员几个的人数从大到小 ...

  4. Haproxy-1.8.20 根据路径(URI)转发到后端不同集群

    HAProxy根据不同的URI 转发到后端的服务器组 1 ) 实验内容说明: 1.1 ) 根据不同的URI 转发到后端的服务器组. /a /b 和其他 默认使用其他. 1.2 ) 使用IP介绍: ha ...

  5. newbee-mall 开源商城新计划:秒杀功能、优惠券、对接支付宝

    新项目是 newbee-mall 的升级版本,暂时就叫它 newbee-mall-plus 吧,第一阶段会开发秒杀功能.优惠券.对接支付宝这些功能,也会慢慢加入 Redis. Elastic Sear ...

  6. 【Oracle】增量备份和全库备份怎么恢复数据库

    1差异增量实验示例 1.1差异增量备份 为了演示增量备份的效果,我们在执行一次0级别的备份后,对数据库进行一些改变. 再执行一次1级别的差异增量备份: 执行完1级别的备份后再次对数据库进行更改: 再执 ...

  7. kubernets之Ingress资源

    一  Ingress集中式的kubernets服务转发控制器 1.1  认识Ingress的工作原理 注意:图片来源于kubernets in action一书,如若觉得侵权,请第一时间联系博主进行删 ...

  8. Netty学习:EventLoop事件机制

    目录 EventLoop是什么 EventLoop适用的场景 Netty中的EventLoop Netty中的大量inEventLoop判断 Netty是如何建立连接并监听端口的-NIOSocketC ...

  9. Junit测试和反射

    Junit单元测试 测试分类 黑盒测试:不需要写代码,给输入值,看程序能否得到输出期望值. 白盒测试:需要些代码,关注程序具体的执行流程. Junit的使用 步骤 定义一个测试类(测试用例). 定义测 ...

  10. 前端中的script标签

    script标签中的重要属性! . 浏览器解析行内脚本的方式决定了它在看到字符串时,会将其当成结束的 标签.想避免这个问题,只需要转义字符"\" ①即可: 要包含外部文件中的 Ja ...