ABC065D Built[最小生成树]
这题和某道最短路题神似。对于任意点对,将他们连边,不如将他们分别沿$x,y$轴方向上点按顺序连起来,这样不仅可能多连通一些点,也花费更低,所以按照最短路那题的连边方式跑一个kruskal就行了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define dbg(x) cerr << #x << " = " << x <<endl
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,):;}
template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,):;}
template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=+;
struct thxorz{
int u,v,w;
thxorz(int u=,int v=,int w=):u(u),v(v),w(w){}
inline bool operator <(const thxorz&A)const{return w<A.w;}
}e[N<<];
int n,tot;
struct stothx{int x,y,id;}A[N];
inline bool cmp1(stothx a,stothx b){return a.x<b.x;}
inline bool cmp2(stothx a,stothx b){return a.y<b.y;}
int anc[N];
ll ans;
inline int get_anc(int x){return anc[x]==x?x:anc[x]=get_anc(anc[x]);} int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
read(n);
for(register int i=;i<=n;++i)read(A[i].x),read(A[i].y),A[i].id=i;
sort(A+,A+n+,cmp1);
for(register int i=;i<n;++i)e[++tot]=thxorz(A[i].id,A[i+].id,A[i+].x-A[i].x);
sort(A+,A+n+,cmp2);
for(register int i=;i<n;++i)e[++tot]=thxorz(A[i].id,A[i+].id,A[i+].y-A[i].y);
sort(e+,e+tot+);
for(register int i=;i<=n;++i)anc[i]=i;
for(register int i=;i<=tot;++i)if(get_anc(e[i].u)^get_anc(e[i].v))ans+=e[i].w,anc[anc[e[i].u]]=anc[e[i].v];
printf("%lld\n",ans);
return ;
}
ABC065D Built[最小生成树]的更多相关文章
- Built(最小生成树+构图离散化)
个人心得:看了题目很明确,最小生成树,但是但是周赛卡住了,因为10W的点若一个一个找出距离很明显内存和时间都炸了, 静下心来,画了下图,仔细一想,任意一个点都只会在她左右俩边选择建立联系,那么我们只要 ...
- timus 1982 Electrification Plan(最小生成树)
Electrification Plan Time limit: 0.5 secondMemory limit: 64 MB Some country has n cities. The govern ...
- POJ 1751 Highways (最小生成树)
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- POJ 2031 Building a Space Station (最小生成树)
Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...
- hdu1102 Constructing Roads (简单最小生成树Prim算法)
Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...
- POJ 2485:Highways(最小生成树&&prim)
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21628 Accepted: 9970 Descrip ...
- POJ2031Building a Space Station (最小生成树之prim)
Problem Description You are a member of the space station engineering team, and are assigned a task ...
- ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法
主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...
随机推荐
- PTA(Basic Level)1033.旧键盘打字
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在 2 行中分别给出坏掉的那些键.以及应该输入 ...
- redis哨兵sentinel.conf文件
关闭保护模式 //17行 protected-mode no 端口号 //21 port 26379 后台启动 //26 daemonize yes //84行 主机的ip加端口号 2 为票数 sen ...
- ApplicationListener原理分析
在 Nacos配置服务原理 文中结束时提到过通过发布 ApplicationListener 刷新事件完成 Context 中属性值的更新.那么本章我们一起分析 ApplicationListener ...
- [Err] 1054 - Unknown error 1054
[Err] 1054 - Unknown error 1054:很小的一个错误,缺耽误很长的时间,字段不匹配: 解决方法:一一对照字段,数据库字段要和类中的字段要对应,或者sql语句中使用的字段!仔细 ...
- mount.nfs: access denied by server while mounting
在利用centos7系统搭建NFS服务时出现如下问题,百度后才解决 因为当时在服务器端vim /etc/exports 时, 我只写了 这一行 /home/wjs-nfs *(ro) (没想到偷懒出 ...
- python-day7(正式学习)
目录 数字类型内置方法 整形内置方法(int) 常用操作+内置方法 是否可变 浮点型内置方法(float) 常用操作+内置方法 是否可变 字符串内置方法 常用操作+内置方法 是否可变 数字类型内置方法 ...
- k8s之statefulSet-有状态应用副本集控制器
1.概述 无状态应用更关注群体,任何一个成员都可以被取代,有状态应用关注的是个体.用deployment控制器管理的nginx.myapp等都属于无状态应用,像mysql.redis.zookeepe ...
- 论文笔记-IGCV3:Interleaved Low-Rank Group Convolutions for Efficient Deep Neural Networks
论文笔记-IGCV3:Interleaved Low-Rank Group Convolutions for Efficient Deep Neural Networks 2018年07月11日 14 ...
- lua加载DLL
.cpp //若没有在项目属性--库文件.依赖文件.包含添加.则添加一下路径 #pragma comment (lib,"lua5.1.lib") #include " ...
- 4-Perl 数据类型
1.Perl 数据类型Perl 是一种弱类型语言,所以变量不需要指定类型,Perl 解释器会根据上下文自动选择匹配类型.Perl 有三个基本的数据类型:标量.数组.哈希.以下是这三种数据类型的说明:1 ...