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 ...
随机推荐
- springboot整合es客户端操作elasticsearch(二)
在上章节中整合elasticsearch客户端出现版本问题进行了处理,这章来进行springboot整合得操作 环境:elaticsearch6.2.1,springboot 2.1.8 客户端版本采 ...
- 第十四周课程总结&实验报告
简单记事本的实现 实验源码 主类 package test1; import javax.swing.JFrame; import javax.swing.JTextArea; public clas ...
- POJ1988 Cube Stacking 【并查集】
题目链接:http://poj.org/problem?id=1988 这题是教练在ACM算法课上讲的一道题,当时有地方没想明白,现在彻底弄懂了. 题目大意:n代表有n个石头,M a, b代表将a石头 ...
- autossh
LDAP:用old HK login SSH转发:/root/.nat_rules/opay-new root@ldap opay-new]# cat maria1-dw-60000 #!/bin/b ...
- python列表和if语句的简单结合
将列表所有元素打印出来 cars = ['toyota', 'honda', 'mazda', 'nissan', 'mitsubishi', 'subaru', 'suzuki', 'isuzu'] ...
- 【转帖】K8s-yaml的使用及命令
K8s-yaml的使用及命令 https://www.cnblogs.com/fuyuteng/p/9460534.html YAML配置文件管理对象 对象管理: # 创建deployment资源 k ...
- Git基本理解
1.版本控制 Git 是一个分布式版本控制系统 (Distributed Version Control System - DVCS). 所谓版本控制,意思就是在文件的修改历程中保留修改历史,让你可以 ...
- Linux-echo:打印彩色输出
脚本可以使用转义序列在终端中生成彩色文本 文本颜色是由对应的色彩码来描述的.其中包括: 重置=0,黑色=30,红色=31,绿色=32, 黄色=33,蓝色=34,洋红=35,青色=36,白色=37. 要 ...
- idea的配置文件------application.properties和application.yml
当application.yml 和 application.properties 两个文件同时存在的时候,application.properties的优先级是高于application.yml的, ...
- IE浏览器中判断IE版本
<!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> 所有的IE可识别 <![e ...