C - Highways poj1751最小生成树
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 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
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
第一次接触不给权值的最小生成树问题,处理方法和原来一样。。
题目大意:首先输入有N个城市,然后按照城市顺序输入城市的坐标。最后输出的是需要在哪两个城市之间添加一下路径,这个题目还有个坑就是,输出的结果,对顺序没有要求。
就是即便把1 3 输出成了3 1 也无所谓的
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N= 1E6+;
int pre[N];
struct stu{
int a,b;
int x;
}p[N],pp[N]; bool cmp(stu x1,stu y1){
if(x1.x!=y1.x)
return x1.x<y1.x;
else if(x1.a!=y1.a){
return x1.a<y1.a;
}
return x1.b<y1.b;
} int find(int x){
if(x==pre[x]) return x;
return pre[x]=find(pre[x]);
}
void join(int x,int y){
int fx=find(x),fy=find(y);
if(fx!=fy){
pre[fx]=fy;
}
} int main(){
int n;
cin>>n; for(int i=;i<=n;i++){
pre[i]=i;
} for(int i=;i<=n;i++){
scanf("%d%d",&p[i].a,&p[i].b);
} int pos=;
for(int i=;i<n;i++){
for(int j=i+;j<=n;j++){
pp[pos].a=i;//i号城市
pp[pos].b=j;//j号城市
pp[pos].x=(p[i].a-p[j].a)*(p[i].a-p[j].a)+(p[i].b-p[j].b)*(p[i].b-p[j].b);//i号城市到j号城市的距离
pos++;
}
}
sort(pp,pp+pos,cmp);
int m;
cin>>m;
int xx,yy;
for(int j=;j<=m;j++) {
scanf("%d%d",&xx,&yy);
join(xx,yy);
}
for(int i=;i<pos;i++){
int fx=find(pp[i].a);
int fy=find(pp[i].b);
if(fx!=fy){
pre[fx]=fy;
cout<<pp[i].a<<" "<<pp[i].b<<endl;
}
}
return ;
}
C - Highways poj1751最小生成树的更多相关文章
- Highways POJ-1751 最小生成树 Prim算法
Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输 ...
- POJ-1751 Highways(最小生成树消边+输出边)
http://poj.org/problem?id=1751 Description The island nation of Flatopia is perfectly flat. Unfortun ...
- POJ1751 Highways【最小生成树】
题意: 给你N个城市的坐标,城市之间存在公路,但是由于其中一些道路损坏了,需要维修,维修的费用与公路长成正比(公路是直的). 但现有M条公路是完整的,不需要维修,下面有M行,表示不需要维修的道路两端的 ...
- POJ 1751 Highways (最小生成树)
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...
- POJ 2485:Highways(最小生成树&&prim)
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21628 Accepted: 9970 Descrip ...
- POJ 1751 Highways 【最小生成树 Kruskal】
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23070 Accepted: 6760 Speci ...
- POJ 2485 Highways( 最小生成树)
题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 2485 Highways (求最小生成树中最大的边)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
随机推荐
- SpringBoot2 + Druid + Mybatis 多数据源动态配置
在大数据高并发的应用场景下,为了更快的响应用户请求,读写分离是比较常见的应对方案.读写分离会使用多数据源的使用.下面记录如何搭建SpringBoot2 + Druid + Mybatis 多数据源配 ...
- 单线程多任务协程vip电影爬取
单线程多任务协程vip电影爬取 --仅供学习使用勿作商用如有违规后果自负!!! 这几天一直在使用python爬取电影,主要目的也是为了巩固前段时间强化学习的网络爬虫,也算是一个不错的检验吧,面对众 ...
- jupyter notebook 中同时添加Python2和3,在conda下配置R语言运行的环境
1.第一步,安装Python2的环境 首先,在安装anaconda的时候先选择一个Python安装,我先安装的是Python3 然后,在anaconda Prompt下创建Python2环境 现在,还 ...
- MySQL count知多少
统计一个表的数据量是经常遇到的需求,但是不同的表设计及不同的写法,统计性能差别会有较大的差异,下面就简单通过实验进行测试(大家测试的时候注意缓存的情况,否则影响测试结果). 1. 准备工作 为了后续测 ...
- mabatis入门五 高级结果映射
一.创建测试的表和数据 1.创建表 1CREATE TABLE items ( 2 id INT NOT NULL AUTO_INCREMENT, 3 itemsname VARCHAR(32) NO ...
- WEB缓存系统之varnish状态引擎
前文我们聊了下varnish的VCL配置以及语法特点,怎样去编译加载varnish的vcl配置,以及命令行管理工具varnishadm怎么去连接varnish管理接口进行管理varnish,回顾请参考 ...
- Django-User
User对象 User对象是认证系统的核心.用户对象通常用来代表网站的用户,并支持例如访问控制.注册用户.关联创建者和内容等.在Django认证框架中只有一个用户类,例如超级用户('superuser ...
- springboot + aop + Lua分布式限流的最佳实践
整理了一些Java方面的架构.面试资料(微服务.集群.分布式.中间件等),有需要的小伙伴可以关注公众号[程序员内点事],无套路自行领取 一.什么是限流?为什么要限流? 不知道大家有没有做过帝都的地铁, ...
- python常用模块 以及第三方导入
python常用模块 1模块的分类 标准模块(内置模块)( 标准库 )300 第三方模块 18万 pip install 直接通过pip安装 软件一般会被自动安装你python安装目录的这个子目录里 ...
- Java并发基础06. 线程范围内共享数据
假设现在有个公共的变量 data,有不同的线程都可以去操作它,如果在不同的线程对 data 操作完成后再去取这个 data,那么肯定会出现线程间的数据混乱问题,因为 A 线程在取 data 数据前可能 ...