题目描述

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 实现每次从list中取5000条数据放入新list

    从list中取固定条数的数据放入新的list里 public static <T> List<List<T>> split(List<T> resLis ...

  2. Arduino串口的一些高级用法

    1.配置串口通信数据位.校验位.停止位通常我们使用Serial.begin(speed)来完成串口的初始化,这种方式,只能配置串口的波特率.而使用Serial.begin(speed, config) ...

  3. HDU 1588 矩阵快速幂 嵌套矩阵

    这个题目搞了我差不多一个下午,之前自己推出一个公式,即 f[n+k]=k*f[n]+f[n-1]结果发现根本不能用,无法降低复杂度. 后来又个博客的做法相当叼,就按他的做法来了 即 最终求得是 S(n ...

  4. 201612-2 工资计算 Java

    思路: 税+税后所得A=税前工资S. 因为工资是整百的数,每次减100来判断.好理解但是超时. import java.util.Scanner; //只有90分,超时了 public class M ...

  5. docker入门1---docker的简介和安装

    Tomxin7 Simple, Interesting | 简单,有趣 什么是Docker? 简介: Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发 ...

  6. Django2.0——中间件

    Django中间件middleware本质是一个类,在请求到返回的中间,类中不同的方法会在指定的时机中被触发.setting.py的变量MIDDLEWARE_CLASSES中的每一个元素都是中间件,且 ...

  7. html分页自适应居中;css设置分页自适应居中

    制作网页列表的分页必不可少,显示的列表条数也不一样,让我们一起来看看如何让分页标签根据给定的分页自动居中呢. 对<ul>标签设置样式为:{ display: table margin:40 ...

  8. 《Docekr入门学习篇》——Docker镜像制作

    Docker镜像制作 Docker镜像的构建分为两种,一种是手动构建,一种是dockerfile(自动构建) 手动构建 基于centos镜像进行构建制作Nginx镜像 [root@rbtnode1 ~ ...

  9. php抓取网站图片源码

    <?php /*完成网页内容捕获功能*/ function get_img_url($site_name){     $site_fd = fopen($site_name, "r&q ...

  10. Ubuntu--- 安装VMware 报错 Build enviroment error!

    今天从 Ubuntu 安装 VMware 下载并安装过程都很顺利,但是在启动过程中报错误,所以总结如下: 报错原因:VMware 第一次启动需要编译一些模块,但是刚开始并没有安装 gcc 所以便报无法 ...