justoj connect(边的处理)
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;
}
justoj connect(边的处理)的更多相关文章
- Connect() 2016 大会的主题 ---微软大法好
文章首发于微信公众号"dotnet跨平台",欢迎关注,可以扫页面左面的二维码. 今年 Connect 大会的主题是 Big possibilities. Bold technolo ...
- IdentityServer4 使用OpenID Connect添加用户身份验证
使用IdentityServer4 实现OpenID Connect服务端,添加用户身份验证.客户端调用,实现授权. IdentityServer4 目前已更新至1.0 版,在之前的文章中有所介绍.I ...
- 2003-Can't connect to mysql server on localhost (10061)
mysql数据库出现2003-Can't connect to mysql server on localhost (10061)问题 解决办法:查看wampserver服务器是否启动,如果没有启动启 ...
- Error connecting to database [Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)]
参照 http://stackoverflow.com/questions/4448467/cant-connect-to-local-mysql-server-through-socket-var- ...
- HTTP Method详细解读(`GET` `HEAD` `POST` `OPTIONS` `PUT` `DELETE` `TRACE` `CONNECT`)
前言 HTTP Method的历史: HTTP 0.9 这个版本只有GET方法 HTTP 1.0 这个版本有GET HEAD POST这三个方法 HTTP 1.1 这个版本是当前版本,包含GET HE ...
- IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护API
IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习之保护API. 使用IdentityServer4 来实现使用客户端凭据保护ASP.N ...
- Connect to the DSP on C6A8168/DM8168/DM8148 using CCS
转自ti-wiki 这份wiki,我曾经就收藏过,但是没有加以重视,以至于绕了一大圈的ccs开发环境的配置,现在正式收藏于自己的博客中...总结良多啊 Connecting to DSP on C6 ...
- Action.c(58): Error -27796: Failed to connect to server "hostname"
分析: 因为负载生成器的性能太好发数据特别快,服务器响应也特别快,从而导致负载生成器的端口在没有timeout之前就全部占满了. 解决方案一: 在负载生成器的注册表HKEY_LOCAL_MACHI ...
- VNC connect:Connection refused(10061)
在Windows机器上使用VNC Viewer访问Linux服务器,有时候会遇到"connect:Connection refused(10061)"这个错误,导致这个错误出现的原 ...
随机推荐
- npm安装加速
1.通过config命令 npm config set registry https://registry.npm.taobao.org npm info underscore (如果上面配置正确这个 ...
- 一文读懂 Redis 分布式部署方案
为什么要分布式 Redis是一款开源的基于内存的K-V型数据库,因为内存访问速度快,一般被用来做系统的缓存. Redis作为单机部署能够支持业务简单,数据量不大的系统需求,但在实际应用中,一旦系统规模 ...
- Passing Reference Data Type Arguments
public void moveCircle(Circle circle, int deltaX, int deltaY) { // code to move origin of circle to ...
- C#客户端通过安全凭证调用webservice
怎么解决给XML Web services 客户端加上安全凭据,从而实现调用安全的远程web方法?首先,有远程web服务Service继承自System.Web.Services.Protocols. ...
- LESS 原理,一款css的预处理程序Less的使用
Less一种动态样式语言,LESS将CSS赋予了动态语言的特性,如变量,继承,运算,函数...LESS 既可以在客户端上运行 (支持IE 6+, Webkit, Firefox),也可以借助Node ...
- Uni-app实战项目注意事项
注意: (1)本地开启端口 App running at: Local: http://localhost:8080/ Network: http://192.168.31.43:8080/ 后台人员 ...
- CentOS7.7 安装并配置JDK 1.8
本文介绍如何在CentOS中安装oracleJDK1.8并配置环境变量 1.下载并安装jdk1.8 进入下载页:https://www.oracle.com/technetwork/java/java ...
- about 蛤蛤
蛤蛤属于蛤蛤门(haha),蛤蛤纲(haha),蛤蛤亚纲(haha),蛤蛤目(haha),蛤蛤总科(haha),蛤蛤科(haha).
- JVM(完成度95%,不断更新)
一.HotSpot HotSpot是最新的虚拟机,替代了JIT,提高Java的运行性能.Java原先是将源代码编译为字节码在虚拟机运行,HotSpot将常用的部分代码编译为本地代码. 对象创建过程 类 ...
- 介绍下 npm 模块安装机制,为什么输入 npm install 就可以自动安装对应的模块?
1. npm 模块安装机制: 发出npm install命令 查询node_modules目录之中是否已经存在指定模块 若存在,不再重新安装 若不存在 npm 向 registry 查询模块压缩包的网 ...