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 ...
随机推荐
- 题解 P2755 【洗牌问题】
这是本人的第一篇题解 请多多宽恕 这一道题其实不要用数组 我们来观察一下n=3时的情况: 原: 1 2 3 4 5 6 4 1 5 2 6 3 2 4 6 1 3 5 1 2 3 4 5 6 我们去观 ...
- Selenium系列(十) - 针对Select下拉框的操作和源码解读
如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...
- 从本地方法栈看到jni调用
我们都知道java虚拟机所管理的内存区域包括方法区,堆,虚拟机栈,本地方法栈,程序计数器. 在<深入理解java虚拟机>中,周志明老师对虚拟机栈进行了讲解,但是对本地方法栈却一笔带过.今天 ...
- python之进程,线程
什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本 ...
- shell大全
1.shell判断文件是否存在 http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html
- NKOJ4238 天天爱跑步(【NOIP2016 DAY1】)
问题描述 小C同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一棵包 ...
- STM32CubeMX的安装
1.下载STM32CubeMX 在ST的官方网站上下载STM32CubeMXXX软件的安装包. 下载的安装包如下图所示.双击SetupSTM32CubeMX-5.0.1.exe. 安装STM32Cub ...
- JAVA设计模式——(2)策略模式
定义 定义一种算法,将每个算法都封装起来,并且使它们之间可以互换.是一种行为类模式. 举例 为了通俗易懂,我们拿各国的税率计算来举例子: 假设当前我们的程序只能支持计算中国和美国的税率: public ...
- 1046 Shortest Distance (20分)
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- pinpoint配置使用
一.下载pinpoint包 从https://github.com/naver/pinpoint/releases 下载 pinpoint-agent.tar.gz pinpoint-collecto ...