built?
题目描述
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.
输入
N
x1 y1
x2 y2
:
xN yN
输出
样例输入
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?的更多相关文章
- 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 ...
- 如何选择PHP框架?
PHP是世界上最受欢迎的编程语言之—.最近发布的PHP7令这种服务器的编程语言比以前变得更好,更稳定了. PHP被广泛应用于重大的项目.例如Facebook就是使用PHP来维护和创建它们的内部系统的. ...
- 微软要如何击败Salesforce?Office365、Azure、Dynamics365 全面布局AI | 双语
微软在上月宣布组建自己的 AI 研究小组.该小组汇集了超过 5000 名计算机科学家和工程师,加上微软内部研究部门,将共同挖掘 AI 技术. 与此同时,亚马逊,Facebook,Google,IBM ...
- 如何在Texstudio内加载语法检查词典?
如何在Texstudio编辑软件内加载"语法检查词典"? How to make dictionary work in TexStudio I am using TexStudio ...
- 网站开启https后加密协议始终是TLS1.0如何配置成TLS1.2?
p { margin-bottom: 0.1in; line-height: 120% } 网站开启https后加密协议始终是TLS1.0如何配置成TLS1.2? 要在服务器上开启 TLSv1.,通常 ...
- HDU2586How far away ?
http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Others) ...
- Xcode7打包,iOS9真机闪退,如何解决?
问:有些项目用xcode7打开运行,打包安装到iOS9设备上程序会闪退. 如果用xcode7以下编译,然后打包到iOS9的设备上就是正常的.这是为什么,关键是,怎么解决? 答:iOS9发布之后,有些a ...
- [转载] 首席工程师揭秘:LinkedIn大数据后台是如何运作的?(一)
本文作者:Jay Kreps,linkedin公司首席工程师:文章来自于他在linkedin上的分享:原文标题:The Log: What every software engineer should ...
- 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 ...
随机推荐
- Java 接口理解
学习Spring有一段时间了,对java也有了一点了解,最不能理解的就是接口, 即使是写了接口并实现了它,依然无法理解它到底有什么用?看了其他几篇博客,总结了一下自己的理解. 在JAVA编程语言中是一 ...
- 个人安装GO1.13.6版本指南手册之搭建环境
因好奇而走进go语言,让你不在只闻其声,不见其形. https://golang.org/doc/install:这里是go语言的官网文档.吃不透英文,终究会被限制在有限的区域,一词词的吃透. 安装包 ...
- dac FDMemTable
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- ArrayList扩容原理分析
1:代码解读和分析 1.1:构造方法分析 1: public ArrayList(int initialCapacity) { ) { this.elementData = new Object[in ...
- 代码杂谈-or符号
看到别人的代码里用了 or, 有点巧用. 记录一下. def func(a,b, context=None): # .... ctx = context or global_context() # . ...
- python获取页面文字信息
# -*- coding: utf- -*- from selenium import webdriver import time, re,requests,os,time,random,traceb ...
- Animate.css 一款强大的预设css3动画库
Animate.css是一个有趣的,跨浏览器的css3动画库.很值得我们在项目中引用. 用法 1.首先引入animate css文件 <head> <link rel="s ...
- 66)vector基础总结
基本知识: 1)vector 样子 其实就是一个动态数组: 2)vector的基本操作: 3)vector对象的默认构造 对于类 添加到 容器中 要有 拷贝构造函数---> 这个注意 ...
- vue实现简单的过滤器
html片段: <script src="https://unpkg.com/vue"></script> <div id="app&quo ...
- ElasticSearch-The number of object passed must be even but was [1]-问题解决
ES版本:6.4.3 1.The number of object passed must be even but was [1] 问题代码: IndexRequest indexRequest = ...