Highways

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

Appoint description: 
System Crawler  (2015-06-02)

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

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 <iostream>
#include <fstream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
int FATHER[SIZE],N,M,NUM;
struct Node
{
int from,to;
double cost;
}G[SIZE * SIZE];
struct
{
int x,y;
}TEMP[SIZE]; void ini(void);
int find_father(int);
void unite(int,int);
bool same(int,int);
void kruskal(void);
bool comp(const Node &,const Node &);
double dis(int,int,int,int);
int main(void)
{
int x,y; while(~scanf("%d",&N))
{
ini();
for(int i = ;i <= N;i ++)
scanf("%d%d",&TEMP[i].x,&TEMP[i].y);
for(int i = ;i <= N;i ++)
for(int j = i + ;j <= N;j ++)
{
G[NUM].from = i;
G[NUM].to = j;
G[NUM].cost = dis(TEMP[i].x,TEMP[i].y,TEMP[j].x,TEMP[j].y);
NUM ++;
}
sort(G,G + NUM,comp);
scanf("%d",&M);
for(int i = ;i <= M;i ++)
{
scanf("%d%d",&x,&y);
unite(x,y);
}
kruskal();
} return ;
} void ini(void)
{
NUM = ;
for(int i = ;i <= N;i ++)
FATHER[i] = i;
} int find_father(int n)
{
if(FATHER[n] == n)
return n;
return FATHER[n] = find_father(FATHER[n]);
} void unite(int x,int y)
{
x = find_father(x);
y = find_father(y); if(x == y)
return ;
FATHER[x] = y;
} bool same(int x,int y)
{
return find_father(x) == find_father(y);
} bool comp(const Node & a,const Node & b)
{
return a.cost < b.cost;
} void kruskal(void)
{
int count = ; for(int i = ;i < NUM;i ++)
if(!same(G[i].from,G[i].to))
{
unite(G[i].from,G[i].to);
printf("%d %d\n",G[i].from,G[i].to);
count ++;
if(count == N - )
break;
}
} double dis(int x_1,int y_1,int x_2,int y_2)
{
return pow(x_1 - x_2,) + pow(y_1 - y_2,);
}

POJ 1751 Highways (最小生成树)的更多相关文章

  1. POJ 1751 Highways(最小生成树Prim普里姆,输出边)

    题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has ...

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

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

  3. POJ 1751 Highways 【最小生成树 Kruskal】

    Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23070   Accepted: 6760   Speci ...

  4. POJ 1751 Highways(最小生成树&Prim)题解

    思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...

  5. POJ 1751 Highways (kruskal)

    题目链接:http://poj.org/problem?id=1751 题意是给你n个点的坐标,然后给你m对点是已经相连的,问你还需要连接哪几对点,使这个图为最小生成树. 这里用kruskal不会超时 ...

  6. (poj) 1751 Highways

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

  7. POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

                                                                                                         ...

  8. POJ 2485 Highways 最小生成树 (Kruskal)

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

  9. POJ 1751 Highways (ZOJ 2048 ) MST

    http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大 ...

随机推荐

  1. JavaScript神一样的变量系统

    话说上一篇介绍了JavaScript故事版的身世之谜.看官你估计也明白JavaScript出生之时,就未曾托于重任.布兰登-艾奇估计也没料到今天的JavaScript变得如此重要.要不然,当年他也不会 ...

  2. PowerDesigner反向生成Mysql数据原型

    PowerDesigner反向生成Mysql数据原型 注意事项: (1)JVM 要32位的. (2)需配置JAVA_HOME环境变量指向所需JVM. (3)需配置CLASSPATH环境变量执行 MyS ...

  3. installshield Basic 工程每次安装完提示重启电脑

     将Sequence中的ScheduleReboot Action的Condition清空即可. 

  4. UserControl和CustomControl基础【PluraSight】

    UserControl UserControl实际上就是ContentControl,xaml里<UserControl></UserControl>tag之间包含的实际就是后 ...

  5. 在VS2012中使用GDI+

    首先说明,在VS的较高版本中,已经包含GDI+的SDK,不用再次下载,只需要使用前应用相应的头文件,添加些代码即可.但是VC6.0中,没有GDI+SDK,需要同志们下载才行. 步骤: 1.在stdaf ...

  6. xhtml和css概述

    Xhtml和css概述 1.html的过渡到xhtml html与xhtml不是两种语言,它们是一种语言的不同阶段,有点类似于文言文和白话文之间的关系.因为网络技术的日新月异,html的不断改进,所以 ...

  7. DropdownList绑定的两种方法

    动态绑定方法一:动态绑定数据库中的字段. SqlConnection conn = UtilitySqlClass.OperateDataBase.ReturnConn();string strSQL ...

  8. javascript------>delete

    delete只能删除属性,不能删除变量 var a = "roboce"; delete a; // false a; b = "haha"; delete b ...

  9. linux 认证方式

  10. BinaryWriter和BinaryReader用法

      C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操作,于是StreamWriter和 StreamReader类增强了FileStream,它让我们在字符串级 ...