题目描述

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. Linux环境创建交换分区

    最近在准备在移动端跑一下深度学习训练好的模型,在RK3399的板子上安装scipy时报错.网上查了一下,由于内存不足导致,做个交换分区就搞定了.那么如何做交换分区呢.话不多说,直接开撸. ------ ...

  2. WEB测试方法总结-笔记

    此文来自公开课培训笔记!!! 一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&符 ...

  3. Window Nginx安装

    1.下载Nginx 下载地址:http://nginx.org/en/download.html 我这里下载的版本是: nginx/Windows-1.12.2 2.解压Nginx 把下载下来的zip ...

  4. Win10 MySQL5.7中文乱码问题

    https://blog.csdn.net/hh___56789/article/details/87900923 最好把 utf8 都换成utf8mb4 ,以免以后遇到意想不到的错误.utf8有漏洞 ...

  5. Java 接口理解

    学习Spring有一段时间了,对java也有了一点了解,最不能理解的就是接口, 即使是写了接口并实现了它,依然无法理解它到底有什么用?看了其他几篇博客,总结了一下自己的理解. 在JAVA编程语言中是一 ...

  6. 干货 | 运维福音——Terraform自动化管理京东云

    干货 | 运维福音--Terraform自动化管理京东云 原创: 张宏伟 京东云开发者社区  昨天 Terraform是一个高度可扩展的IT基础架构自动化编排工具,主张基础设施即代码,可通过代码集中管 ...

  7. B - Given Length and Sum of Digits... CodeForces - 489C (贪心)

    You have a positive integer m and a non-negative integer s. Your task is to find the smallest and th ...

  8. Java学习十三

    学习内容: 1.Java反射 2.jdbc入门 1.反射的概述 Java的反射机制:动态获取信息以及动态调用对象方法 Java的反射机制的作用:用来编写一些通用性较高的代码或者框架的时候使用 原理:j ...

  9. 面试准备 DOM

    基本概念:Dom事件的级别 Dom0 级别 element.onclick=function() {} Dom1  没有制定事件相关的 Dom2 element.addEventListener(&q ...

  10. 思考题:clock类编写

    题目 为便于后文理解,这里先补上这份代码前文开的库以及宏定义: #include<cstdio> #include<iostream> #include<string&g ...