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 ...
随机推荐
- Linux远程开发
Linux远程开发 通常,当我们开发Linux程序时有两种方案: 在Linux上直接编写程序并进行运行测试和调试 在Windows或Mac OS X上借助工具进行远程开发 虽然我自己是在Linux环境 ...
- Element-UI 框架 el-scrollbar 组件
Element-UI 框架 el-scrollbar 组件:https://juejin.im/post/5c83d5ac5188257e1c4dc9e7
- vue加scoped后无法修改样式(无法修改element UI 样式)
有的时候element提供的默认的样式不能满足项目的需要,就需要我们队标签的样式进行修改,但是发现修改的样式不起作用 第一种方法 原因:scoped 解决方法:去掉scoped 注意:此时该样式会污染 ...
- 图表:WebChartControl
#region 画统计图 /// <summary> /// 画统计图 /// </summary> private void LoadWebChartControl() { ...
- IntelliJ IDEA打jar时常遇见的问题
1.打包的时候提示 manifest.mf already exists in vfs 解决方案:这个文件夹删除掉,再重新build打包即可 2. 第一个选择“extract to the targ ...
- Thinkphp 使用小结
分页中带查询参数 ...->paginate(15,false,['query'=>request()->param()]); 队列后台自动开启运行 nohup php think ...
- Spring IOC的底层实现原理
PS:模块之间的相互依赖叫做耦合 传统方式的开发 UserService us=new UserService(); || v 面向接口编程 UserService us=new UserServi ...
- vim 常用基本
vim 基本操作 0. 基本操作 :w // 保存当前文件 :q // 退出vim :wq // 保存退出 :w! // 强制保存当前文件 :q! // 强制退出(可以忽略修改) :!cmd // 执 ...
- Linux就该这么学——初识管道符
初识管道命令符 管道命令符本质(就是一个“任意门”) 把前一个命令原本要输出到屏幕的标准正常数据当做是最后一个命令的标准输入 格式 : “命令A | 命令B | ...” 示例 : 1.找出被限制登录 ...
- 购物车以php原生cookie实现
index.php //入口文件 <?php /** * @name index.php * @decs * @author 老猫 <18368091722@163.com> * U ...