题目描述

There are N towns on a plane. The i-th town is located at the coordinates (xi,yi). There may be more than one town at the same coordinates.
You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a−c|,|b−d|) yen (the currency of Japan). It is not possible to build other types of roads.
Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints
2≤N≤105
0≤xi,yi≤109
All input values are integers.

输入

Input is given from Standard Input in the following format:

N
x1 y1
x2 y2
:
xN yN

输出

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

样例输入

3
1 5
3 9
7 8

样例输出

3

提示

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

今日大凶......总之审题,数据范围都弄不清了

这题初看感觉是最短路,但是每个点连起来就是1e10了

题意是把每个点连起来,需要花费多少,花费就是min(|a−c|,|b−d|)

所以并不需要计算每个点的距离,只需x和y分别排序一下就好,最短距离只有可能从按大小排序的这些点的距离选

然后是Kruskal算法 https://blog.csdn.net/liangzhaoyang1/article/details/51169090

(1) 将全部边按照权值由小到大排序。
(2) 按顺序(边权由小到大的顺序)考虑每条边,只要这条边和我们已经选择的边不构成圈,就保留这条边,否则放弃这条边。

#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+;
const int maxn=1e5+;
int book[maxn],n;
//
void init()
{
for(int i=;i<=n;i++) book[i]=i;
}
int getfa(int x){
if(book[x]!=x) book[x]=getfa(book[x]);
return book[x];
}
void mergexy(int x,int y)
{
book[y]=x;
}
//
struct flv
{
int u,v,z;
}f[maxn*];
struct node
{
int x,y,num;
}s[maxn];
bool cmp1(node a,node b)
{
return a.x<b.x;
}
bool cmp2(node a,node b)
{
return a.y<b.y;
}
bool cmp3(flv a,flv b)
{
return a.z<b.z;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d %d",&s[i].x,&s[i].y);
s[i].num=i;
}
sort(s+,s+n+,cmp1);
int cnt=;
for(int i=;i<n;i++){
f[++cnt].u=s[i].num;
f[cnt].v=s[i+].num;
f[cnt].z=min(s[i+].x-s[i].x,abs(s[i+].y-s[i].y));
}
sort(s+,s+n+,cmp2);
for(int i=;i<n;i++){
f[++cnt].u=s[i].num;
f[cnt].v=s[i+].num;
f[cnt].z=min(s[i+].y-s[i].y,abs(s[i+].x-s[i].x));
}
sort(f+,f+cnt+,cmp3);
init();
int number=;
int sum=;
for(int i=;i<=cnt;i++){
int p=getfa(f[i].u),q=getfa(f[i].v);
if(p!=q){
mergexy(p,q);
number++;
sum+=f[i].z;
}
if(number==n) break;
}
printf("%d\n",sum);
return ;
}

built?的更多相关文章

  1. ROS常见问题(三) 报错are you sure it is properly registered and that the containing library is built?

    报错: [FATAL] [1576042404.913706482]: Failed to create the global_planner/GlobalPlanner planner, are y ...

  2. 如何选择PHP框架?

    PHP是世界上最受欢迎的编程语言之—.最近发布的PHP7令这种服务器的编程语言比以前变得更好,更稳定了. PHP被广泛应用于重大的项目.例如Facebook就是使用PHP来维护和创建它们的内部系统的. ...

  3. 微软要如何击败Salesforce?Office365、Azure、Dynamics365 全面布局AI | 双语

    微软在上月宣布组建自己的 AI 研究小组.该小组汇集了超过 5000 名计算机科学家和工程师,加上微软内部研究部门,将共同挖掘 AI 技术. 与此同时,亚马逊,Facebook,Google,IBM ...

  4. 如何在Texstudio内加载语法检查词典?

    如何在Texstudio编辑软件内加载"语法检查词典"? How to make dictionary work in TexStudio I am using TexStudio ...

  5. 网站开启https后加密协议始终是TLS1.0如何配置成TLS1.2?

    p { margin-bottom: 0.1in; line-height: 120% } 网站开启https后加密协议始终是TLS1.0如何配置成TLS1.2? 要在服务器上开启 TLSv1.,通常 ...

  6. HDU2586How far away ?

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Others) ...

  7. Xcode7打包,iOS9真机闪退,如何解决?

    问:有些项目用xcode7打开运行,打包安装到iOS9设备上程序会闪退. 如果用xcode7以下编译,然后打包到iOS9的设备上就是正常的.这是为什么,关键是,怎么解决? 答:iOS9发布之后,有些a ...

  8. [转载] 首席工程师揭秘:LinkedIn大数据后台是如何运作的?(一)

    本文作者:Jay Kreps,linkedin公司首席工程师:文章来自于他在linkedin上的分享:原文标题:The Log: What every software engineer should ...

  9. hdu----(2586)How far away ?(DFS/LCA/RMQ)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. C# 串口编程,扫码枪使用

    一.串口通信简介 串行接口(串口)是一种可以将接受来自CPU的并行数据字符转换为连续的串行数据流发送出去,同时可将接受的串行数据流转换为并行的数据字符供给CPU的器件.一般完成这种功能的电路,我们称为 ...

  2. request对象和response对象的作用和相关方法

    response对象(响应) 响应行 状态码 :setStatus(int a) 设置状态码 302重定向 304控制缓存 响应头 setHeader() 一个key对应一个value addHead ...

  3. Vue-router(4)之路由跳转

    路由传参 案例:现在需要展示一个电影列表页,点击每一部电影,会跳转到该部电影详情页(跳转时携带type和id) 代码实现(未携带type): index.js import Vue from 'vue ...

  4. vue表单选项框

    选项框选的内容在下面显示 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  5. 吴裕雄--天生自然 JAVASCRIPT开发学习: JSON

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. Android前后台切换的监听

    本文参考这位哥们:https://juejin.im/post/5b87f409e51d4538b0640f58 首先写两个类文件ActivityLifecycleCallbacksAdapter.L ...

  7. 第二季 第四集 part3

    obj.insertAdjancetHtlm("beforeend"(位置), r(内容)) insertAdjacentHTML() 将指定的文本解析为HTML或XML,并将结果 ...

  8. shell中通过sed替换文件中路径

    通常sed指令修改行内容时使用:sed -i " 9 s/^.*/"type in what you want modified!"/" 其中"typ ...

  9. JXCPC 试题册

    JXCPC 试题册 Input file: standard input Output file: standard output Time limit: 1s Memory limit: 256 m ...

  10. BZOJ 4913 [Sdoi2017] 遗忘的集合

    骂了隔壁的 BZOJ垃圾评测机 我他妈卡了两页的常数了 我们机房的电脑跑的都比BZOJ快