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 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 should be created but it should be empty.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input
1

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个城镇的坐标(x。y)  m条已经修好的路(公路一端城镇编号,还有一端编号)。

输出格式为:(待建公路一端城镇编号,还有一端编号)特殊判题,不用在意输出顺序。

注意:相邻两组測试例子的输出要有空行。最后一组后不加空行。不然就WR,居然还不是PE。zoj不够友好。

//zoj-2048-ac  special judge
#include
#include
#include
#define M 750*750/2
using namespace std;
int n,m;//共n个点,已有m条边
struct edge {
int v1,v2;
double dist;
};
struct vertice{
int x,y;
};
vertice arr_v[M];
priority_queue pq;
int tree[M];
bool operator<(const edge&a,const edge&b){
if(a.dist>b.dist) return true;//保证pq.top()是当前最小的
return false;
}
int f_find_root(int x){
if(tree[x]==-1)
return x;
else{
int tmp=f_find_root(tree[x]);
tree[x]=tmp;
return tmp;
}
}//f_find_root
void f_union(int a,int b){
a=f_find_root(a);
b=f_find_root(b);
if(a!=b) tree[b]=a;
}//f_union
void f_init(){
for(int i=0;i>n;vertice tmpv;
for(int i=0;i>arr_v[i].x>>arr_v[i].y;
cin>>m;
int v1,v2;
for(int i=0;i>v1>>v2; v1--;v2--;
f_union(v1,v2);
}
}
void f_calc(){
edge tmpe;double dx,dy;
int v1,v2;
for(int i=0;i>ncase;
while(ncase--){
f_init();
f_in();
f_calc();
if(ncase) cout<

 

最小生成树-并查集-Kruskal-zoj-2048-special judge的更多相关文章

  1. UVA 1395 苗条的生成树(最小生成树+并查集)

    苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...

  2. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  3. CSP 201703-4 地铁修建【最小生成树+并查集】

    问题描述 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市 ...

  4. 关于最小生成树(并查集)prime和kruskal

    适合对并查集有一定理解的人.  新手可能看不懂吧.... 并查集简单点说就是将相关的2个数字联系起来 比如 房子                      1   2    3   4  5   6 ...

  5. poj1861 network(并查集+kruskal最小生成树

    题目地址:http://poj.org/problem?id=1861 题意:输入点数n和边数n,m组边(点a,点b,a到b的权值).要求单条边权值的最大值最小,其他无所谓(所以多解:(.输出单条边最 ...

  6. (最小生成树 并查集)P1111 修复公路 洛谷

    题目背景 A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 题目描述 给出A地区的村庄数N,和公路数M,公路是双向的.并告诉你每条公路的连着哪两个村庄,并告诉你什么时 ...

  7. NOI2018Day1T1 归程 并查集 kruskal kruskal重构树 倍增表 Dijkstra

    原文链接https://www.cnblogs.com/zhouzhendong/p/NOI2018Day1T1.html 题目传送门 - 洛谷P4768 题意 给定一个无向连通图,有 $n$ 个点 ...

  8. [NOI2018]归程(可持久化并查集,Kruskal重构树)

    解法一: 1.首先想到离线做法:将边和询问从大到小排序,并查集维护连通块以及每个连通块中所有点到1号点的最短距离.$O(n\log n)$ 配合暴力等可以拿到75分. 2.很容易想到在线做法,使用可持 ...

  9. 【BZOJ4144】[AMPPZ2014]Petrol(最短路+最小生成树+并查集)

    Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油站可以补满. q次询问,每次给出x,y,b,表示出发点是 ...

  10. bzoj 3559: [Ctsc2014]图的分割【最小生成树+并查集】

    读题两小时系列-- 在读懂题意之后,发现M(c)就是c这块最大权割边也就是的最小生成树的最大权边的权值,所以整个问题都可以在MST的过程中解决(M和c都是跟着并查集变的) 不过不是真的最小生成树,是合 ...

随机推荐

  1. 本地代码中使用Java对象

    通过使用合适的JNI函数,你可以创建Java对象,get.set 静态(static)和 实例(instance)的域,调用静态(static)和实例(instance)函数.JNI通过ID识别域和方 ...

  2. spark 类标签的稀疏 特征向量

    一个向量(1.0,0.0,3.0)它有2中表示的方法 密集:[1.0,0.0,3.0]    其和一般的数组无异 稀疏:(3,[0,2],[1.0,3.0])     其表示的含义(向量大小,序号,值 ...

  3. hpuoj--校赛--考试来了(水题)

    问题 C: 感恩节KK专场--考试来了 时间限制: 1 Sec  内存限制: 128 MB 提交: 475  解决: 112 [提交][状态][讨论版] 题目描述 很多课程马上就结课了,随之而来的就是 ...

  4. HBase框架基础(三)

    * HBase框架基础(三) 本节我们继续讨论HBase的一些开发常识,以及HBase与其他框架协调使用的方式.在开始之前,为了框架之间更好的适配,以及复习之前HBase的配置操作,请使用cdh版本的 ...

  5. 浅谈贝塞尔曲线以及iOS中粘性动画的实现

    关于贝塞尔曲线,网上相关的文章很多,这里我主要想用更简单的方法让大家理解贝塞尔曲线,当然,这仅仅是我个人的理解,如有错误的地方还请大家能够帮忙指出来,这样大家才能一起进步. 贝塞尔曲线,常用到的可分为 ...

  6. 添加本地 yum源

    添加本地 yum源 yum-updatesd.conf yum的主配置文件   [root@zhou ~]# cd /etc/yum.repos.d/ [root@zhou yum.repos.d]# ...

  7. 有用的 Bash 快捷键清单

    作者: Sk 译者: LCTT Sun Yongfei 现如今,我在终端上花的时间更多,尝试在命令行完成比在图形界面更多的工作.随着时间推移,我学了许多 BASH 的技巧.这是一份每个 Linux 用 ...

  8. ES6学习4 变量的解构赋值

    变量的解构赋值 一.数组结构赋值 1.数组结构赋值 let [a, b, c] = [1, 2, 3]; ES6 可以从数组中提取值,按照对应位置,对变量赋值. 1)  本质上,这种写法属于“模式匹配 ...

  9. 洛谷P2196 && caioj 1415 动态规划6:挖地雷

    没看出来动规怎么做,看到n <= 20,直接一波暴搜,过了. #include<cstdio> #include<cstring> #include<algorit ...

  10. 紫书 例题 10-7 UVa 10820 (欧拉函数)

    这道题要找二元组(x, y) 满足1 <= x, y <= n 且x与y互素 那么我就可以假设x < y, 设这时答案为f(n) 那么答案就为2 * f(n) +1(x与y反过来就乘 ...