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)"这个错误,导致这个错误出现的原 ...
随机推荐
- curl模拟调用接口
curl模拟调用接口 1. get请求 curl -i -X GET http://url/bind/agentOnWork/v2?Sig=******* 2. post请求(带头信息以及参数) cu ...
- asp .net core发布订阅kafka
Kafka是一种高吞吐量的分布式发布订阅消息系统,有如下特性: 通过O的磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能. 高吞吐量:即使是非常普通的硬件Ka ...
- Python之浅谈模块
目录 模块的四种形式 什么是模块 import和from......import 循环导入 模块的搜索路径 Python文件的两种用途 random模块 模块的四种形式 什么是模块 模块就是一个p ...
- apache frpClien操作报错解决
#打开配置文件vim /etc/vsftpd/vsftpd.conf #修改配置100行chroot_local_user=NO
- python中lambda匿名函数与函数之间的关系
- python-循环-两种方法实现九九乘法表
方法一:用最基本的while循环嵌套(基础时,便于理解) while循环的嵌套,先执行里边的,再执行外边的 i = 1 while i <= 9: j = 1 while j <= i: ...
- Blazor带我重玩前端(二)
概览 Blazor目前有两种托管模式,一种是Server-Side模式,一种是WebAssembly模式.官方首先支持的是Service-Side模式,使用WebAssembly模式,需要更新到最新版 ...
- 数组中出现次数超过一半的数字(剑指offer-28)
题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
- python学习笔记之函数(方法)(四)
一.函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很多不同的叫法.在C中叫function,在Java里面叫做method. 定义: ...
- java IO流 (五) 转换流的使用 以及编码集
转换流的使用 1.转换流涉及到的类:属于字符流InputStreamReader:将一个字节的输入流转换为字符的输入流解码:字节.字节数组 --->字符数组.字符串 OutputStreamWr ...