CONNECT

https://oj.ismdeep.com/contest/problem?id=1702&pid=2

Problem Description

有nn个顶点,每个顶点有自己的坐标(Xi,Yi,Zi)(Xi,Yi,Zi),现在想把这nn个顶点连接起来,已知连接两个顶点uu和vv的花费是MIN(|Xu−Xv|,|Yu−Yv|,|Zu−Zv|) 。现在,请花费最小的代价把这些点连接起来。

Input

第一行输入一个整数n (1≤n≤2∗105)n (1≤n≤2∗105)

接下来nn行,第ii行包含33个整数XiXi,YiYi和ZiZi(|Xi|,|Yi|,|Zi|≤109)(|Xi|,|Yi|,|Zi|≤109),代表第ii个点的坐标,数据保证不会有任意两个点的坐标相同

Output

输出最小代价

Sample Input

3
1 1 1
2 3 4
5 5 5

Sample Output

2

思路:本质是最小生成树。需要对边进行处理。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 200000 + 5; struct Edge
{
ll u,v,dis;
}; struct Point
{
ll x,y,z,index;
};
Edge edge[maxn*3];
Point point[maxn]; int fa[maxn];
ll edgenum;
ll res; bool cmpx(Point a,Point b)
{
return a.x<b.x;
} bool cmpy(Point a,Point b)
{
return a.y<b.y;
} bool cmpz(Point a,Point b)
{
return a.z<b.z;
} bool cmpdis(Edge a,Edge b)
{
return a.dis<b.dis;
} int find(int x)
{
while(x!=fa[x])
x=fa[x]=fa[fa[x]];
return x;
} int main()
{
ll n;
scanf("%lld",&n);
for(int i=0;i<n;i++)
{
scanf("%lld%lld%lld",&point[i].x,&point[i].y,&point[i].z);
point[i].index=i;
}
edgenum=0;
sort(point,point+n,cmpx);
for(int i=0;i<n-1;i++)
{
edge[edgenum].u=point[i+1].index;
edge[edgenum].v=point[i].index;
edge[edgenum].dis=point[i+1].x-point[i].x;
edgenum++;
}
sort(point,point+n,cmpy);
for(int i=0;i<n-1;i++)
{
edge[edgenum].u=point[i+1].index;
edge[edgenum].v=point[i].index;
edge[edgenum].dis=point[i+1].y-point[i].y;
edgenum++;
}
sort(point,point+n,cmpz);
for(int i=0;i<n-1;i++)
{
edge[edgenum].u=point[i+1].index;
edge[edgenum].v=point[i].index;
edge[edgenum].dis=point[i+1].z-point[i].z;
edgenum++;
}
sort(edge,edge+edgenum,cmpdis); //把每条边从小到大排 for(int i=0;i<n;i++)
fa[i]=i;
int eu,ev,cnt=0;
for(int i=0;i<edgenum;i++)
{
eu=find(edge[i].u);
ev=find(edge[i].v);
if(eu!=ev){
fa[ev]=eu;
res+=edge[i].dis;
cnt++;
}
if(cnt==n-1) break;
}
printf("%lld\n",res);
return 0;
}

最新文章

  1. 跟着百度学PHP[4]OOP面对对象编程-12-抽象类
  2. Dynamic CRM 2013学习笔记(二十二)插件里调用WCF服务
  3. Android 源码编译错误
  4. 启动Automatic Updates出现0x80004015错误的解决办法
  5. C# 常用加密处理
  6. Generic repository pattern and Unit of work with Entity framework
  7. python 网络编程(三)---TCP 服务器端客户端实现
  8. sqlite3使用教程1 SQLite 命令
  9. HBase Maven 工程模块梳理
  10. SQL 列拆分
  11. Ionic在Android上部署app步骤
  12. Jquery DataTable AJAX跨域请求的解决方法及SSM框架下服务器端返回JSON格式数据的解决方法
  13. 微信小程序轮播图
  14. 【WebAPI No.4】Swagger实现API文档功能
  15. Java实现带logo的二维码
  16. imu内参标定
  17. 自动读取虚拟币ETC行情并语音提醒的小工具(mac OSX)
  18. YLZ开发外网前端
  19. Isilon上数据是如何存放的?
  20. 【代码审计】CLTPHP_v5.5.3后台任意文件删除漏洞分析

热门文章

  1. mybatis面试入门
  2. 关于对Entity Framework 3.1的理解与总结
  3. 使用IDEA 发布项目搭配远程仓库 Gitee
  4. msf stagers开发不完全指北(二)
  5. node+ajax实战案例(1)
  6. 优雅关闭服务下线(Jetty)
  7. js 字符串排序
  8. Windows系统VSCode、VBox搭建C/C++开发环境
  9. kubernetes-pod驱逐机制
  10. Python-用xlrd模块读取excel,数字都是浮点型,日期格式是数字的解决办法