最小生成树-并查集-Kruskal-zoj-2048-special judge
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的更多相关文章
- UVA 1395 苗条的生成树(最小生成树+并查集)
苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...
- HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
- CSP 201703-4 地铁修建【最小生成树+并查集】
问题描述 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市 ...
- 关于最小生成树(并查集)prime和kruskal
适合对并查集有一定理解的人. 新手可能看不懂吧.... 并查集简单点说就是将相关的2个数字联系起来 比如 房子 1 2 3 4 5 6 ...
- poj1861 network(并查集+kruskal最小生成树
题目地址:http://poj.org/problem?id=1861 题意:输入点数n和边数n,m组边(点a,点b,a到b的权值).要求单条边权值的最大值最小,其他无所谓(所以多解:(.输出单条边最 ...
- (最小生成树 并查集)P1111 修复公路 洛谷
题目背景 A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 题目描述 给出A地区的村庄数N,和公路数M,公路是双向的.并告诉你每条公路的连着哪两个村庄,并告诉你什么时 ...
- NOI2018Day1T1 归程 并查集 kruskal kruskal重构树 倍增表 Dijkstra
原文链接https://www.cnblogs.com/zhouzhendong/p/NOI2018Day1T1.html 题目传送门 - 洛谷P4768 题意 给定一个无向连通图,有 $n$ 个点 ...
- [NOI2018]归程(可持久化并查集,Kruskal重构树)
解法一: 1.首先想到离线做法:将边和询问从大到小排序,并查集维护连通块以及每个连通块中所有点到1号点的最短距离.$O(n\log n)$ 配合暴力等可以拿到75分. 2.很容易想到在线做法,使用可持 ...
- 【BZOJ4144】[AMPPZ2014]Petrol(最短路+最小生成树+并查集)
Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油站可以补满. q次询问,每次给出x,y,b,表示出发点是 ...
- bzoj 3559: [Ctsc2014]图的分割【最小生成树+并查集】
读题两小时系列-- 在读懂题意之后,发现M(c)就是c这块最大权割边也就是的最小生成树的最大权边的权值,所以整个问题都可以在MST的过程中解决(M和c都是跟着并查集变的) 不过不是真的最小生成树,是合 ...
随机推荐
- Getting Started with MongoDB (MongoDB Shell Edition)
https://docs.mongodb.com/getting-started/shell/ Overview Welcome to the Getting Started with MongoDB ...
- hpuoj--校赛--与学妹滑雪(最短路+精度判断)
问题 G: 感恩节KK专场--与学妹滑雪 时间限制: 1 Sec 内存限制: 128 MB 提交: 284 解决: 25 [提交][状态][讨论版] 题目描述 这周下的雪好大好大,不过这正和KK学 ...
- HDFS的配额
- 《剑指offer》树的子结构
一.题目描述 输入两颗二叉树A,B,判断B是不是A的子结构. 二.输入描述 输入两棵树A,B. 三.输出描述 判断B是不是A的子结构,是返回true,否则返回false. 四.牛客网提供的框架 /* ...
- logsource and ALO
1.首先配置sourcedb上的nfs服务,oggstd上挂载sourcedb的online redo和archive log的目录 oggsource上配置: vi /etc/export ...
- ORM原理
原理: 1.实现JavaBean的属性到数据库表的字段的映射: --通过配置文件将JavaBean的属性与数据库表的字段的关联起来 2.映射关系: 一对多,多对一等 持久层(Pers ...
- WLAN STA/AP 并发
WLAN STA/AP 并发 Android 9 引入了可让设备同时在 STA 和 AP 模式下运行的功能.对于支持双频并发 (DBS) 的设备,此功能让一些新功能得以实现,例如在用户想要启用热点 ( ...
- Ubuntu18.04 解压zip文件乱码的解决方法
在Ubuntu的系统下解压zip文件的时候居然出现了乱码,通过查找网上的资料,解决的办法有两种 一.通过unzip行命令解压,指定字符集,由于zip格式中并没有指定编码格式,Windows下生成的zi ...
- Linux系统启动U盘制作工具
1.UNetbootin UNetbootin 让你创建 Ubuntu 或者其他 Linux 发行版的可引导 Live U 盘,而无需烧录 CD. 你既能让 UNetbootin 为你下载众多开箱即用 ...
- linux学习之多高并发服务器篇(一)
高并发服务器 高并发服务器 并发服务器开发 1.多进程并发服务器 使用多进程并发服务器时要考虑以下几点: 父最大文件描述个数(父进程中需要close关闭accept返回的新文件描述符) 系统内创建进程 ...