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. 常见的DNS攻击——偷(劫持)、骗(缓存投毒)、打(DDos)

    常见的DNS攻击包括: 1) 域名劫持 通过采用黑客手段控制了域名管理密码和域名管理邮箱,然后将该域名的NS纪录指向到黑客可以控制的DNS服务器,然后通过在该DNS服务器上添加相应域名纪录,从而使网民 ...

  2. BZOJ 2342 Manacher

    思路: 首先用manacher可以求出以i和i+1中间为对称轴,最长回文串能扩增的长度p[i]. 然后4*(y-x)能更新答案,当且仅当y≤x+p[x]/2且y-p[y]≤x. 按i-p[i]将所有点 ...

  3. Golang 在 Mac、Linux、Windows 下交叉编译

    Golang 支持在一个平台下生成另一个平台可执行程序的交叉编译功能. Mac下编译Linux, Windows平台的64位可执行程序: CGO_ENABLED= GOOS=linux GOARCH= ...

  4. PostgreSQL Replication之第九章 与pgpool一起工作(2)

    9.2 理解pgpool的功能 pgpool提供了如下功能: •连接池 •语句级别的复制 •负载均衡 •限制连接 •内存缓存 •并行查询 [当决定使用那些功能的时候,记住并非所有的功能可以在同一时间使 ...

  5. Css border样式

    1 四个边框 border-left 设置左边框,一般单独设置左边框样式使用border-right 设置右边框,一般单独设置右边框样式使用border-top 设置上边框,一般单独设置上边框样式使用 ...

  6. HDU-1032 The 3n+1 problem 模拟问题(水题)

    题目链接:https://cn.vjudge.net/problem/HDU-1032 水题 代码 #include <cstdio> #include <algorithm> ...

  7. 【Educational Codeforces Round 37 F】SUM and REPLACE

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 那个D函数它的下降速度是很快的. 也就是说到最后他会很快的变成2或者1 而D(2)==2,D(1)=1 也就是说,几次操作过后很多数 ...

  8. JavaScript语言基础3

     JavaScript能够处理一些来自于现实世界的数据类型.比如:数字和文本. 同一时候JavaScript中也包括了一些具 有抽象性质的数据类型.比如对象数据类型. JavaScript它是一种弱类 ...

  9. Ural 1303 Minimal Coverage(贪心)

    题目地址:Ural 1303 先按每一个线段的左端点排序,然后设置一个起点s.每次都从起点小于等于s的线段中找到一个右端点最大的. 并将该右端点作为新的起点s,然后继续找. 从左到右扫描一遍就可以. ...

  10. nyoj 1104 just for you

    just for you 时间限制:1000 ms  |  内存限制:65535 KB 难度:0 描写叙述 今天tlp和ly想去看电影了到了电影院才发现买票的人特别多 .ly不想让tlp等着急了,就先 ...