题目描述

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. 前后端分离java、jwt项目进行CORS跨域、解决非简单请求跨域问题、兼容性问题

    情况描述: 最近在部署一个前后端分离的项目出现了跨域问题*, 项目使用jwt进行鉴权,需要前端请求发起携带TOKEN的请求*,请求所带的token无法成功发送给后端, 使用跨域后出现了兼容性问题:Ch ...

  2. springBoot中的邮件发送

    1. 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  3. 传输层上的TCP和UDP

    参考: 知乎 传输层概述 “三次握手,四次挥手”你真的懂吗? 传输层上的TCP和UDP TCP/IP协议是一个协议簇.里面包括很多协议的,UDP只是其中的一个, 之所以命名为TCP/IP协议,因为TC ...

  4. HGP|VCG|UK10K|中科院职业人群队列研究计划|药物基因组学

    全球性计划:表观组计划:肝计划 测1000人的变异level HGP计划三个阶段,范围逐步扩大和深化. Pilot:deep sequence---low coverage Phase 1 Phase ...

  5. Linux 杀掉所有Java进程

      ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs kill -9 管道符"|"用来隔开两个命令,管道符左 ...

  6. JAVA中如何判断一个输入是数字(小数和整数)还是字符串?

    public class Test1 {     public static void main(String[] args) {         Scanner input = new Scanne ...

  7. Linux 配置单机yum源--ISO镜像做源

    前提:防火墙关闭.SElinus关闭 1.上传ISO镜像(建议传到home目录下) [root@localhost home]# ls iso/ CentOS-.iso 2.挂载目录 [root@lo ...

  8. error: snap "electronic-wechat" has "install-snap" change in progress

    今天因为要使用 wechat ,但是因为 wechat 并没有官方的 Ubuntu 版本,幸好有大神出了 electronic-wechat ,可以直接在应用商店中搜到,然后直接安装,也可以命令行安装 ...

  9. 利用GetCharWidth32获取字符串宽度2

    /////////////////////////////////////////////////////////////// // 04FirstWindow.cpp文件 #include < ...

  10. 7.docker file 语法

    详细文档 : https://docs.docker.com/engine/reference/builder/ 1. FROM   尽量使用官方的 image 作为 base image FROM ...