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 ...
随机推荐
- 二分查找算法C++实现
/************************************************************************* > File Name: binary_se ...
- SQLite基础-4.数据定义语言(DDL)
目录 一.创建数据库 1. 创建方式 2. 数据库命名规范 二. 创建表 1. 基本用法 2. 数据表命名规范 3. 字段命名规范 三. 删除表 一.创建数据库 1. 创建方式 在第二章中我们讲了如何 ...
- java中的 |=、&=、^=
|= 关于 |= 运算符:|= 运算符和 += 这一类的运算符一样,拆解开就是 a = a | b: 代码如下: public static strictfp void main(String[] ...
- SOS--DP(基础版本)未压缩空间
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio>//sprintf islower isupp ...
- T100错误信息提示方式
例如: IF g_browser_cnt = THEN INITIALIZE g_errparam TO NULL LET g_errparam.extend = "" LET g ...
- Censoring(栈+KMP)
# 10048. 「一本通 2.2 练习 4」Censoring [题目描述] 给出两个字符串 $S$ 和 $T$,每次从前往后找到 $S$ 的一个子串 $A=T$ 并将其删除,空缺位依次向前补齐,重 ...
- centos7 源码安装 MongoDb
1.下载源码包 curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.12.tgz 2.解压 放到 /usr/local/ ...
- java——ArrayList中常见方法用法
package com.xt.list; import java.util.ArrayList; import java.util.Iterator; import java.util.List; p ...
- 如何配置数据库镜像<一>
一.简介 “数据库镜像”是Sql Server 2005推出的一个主要用于提高数据库可用率的软件解决方案.镜像是基于每个数据库执行的,仅适用于使用完整恢复模式的数据库.简单恢复模式和大容量日志恢复模式 ...
- C#键盘事件
一: protected override void OnKeyDown(KeyEventArgs e) { if (e.Key==Key.Enter) { sendAppToServer(); } ...