Built(最小生成树+构图离散化)
个人心得:看了题目很明确,最小生成树,但是但是周赛卡住了,因为10W的点若一个一个找出距离很明显内存和时间都炸了,
静下心来,画了下图,仔细一想,任意一个点都只会在她左右俩边选择建立联系,那么我们只要对做表X,Y分别排序然后再构建
距离,然后Kruaskal就OK了。
希望以后思维能够更活跃点,才能不失初望。
题目:
There are N towns on a plane. The i-th town is located at the coordinates (xi,yi). There may be more than one town at the same coordinates.
You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a−c|,|b−d|) yen (the currency of Japan). It is not possible to build other types of roads.
Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?
Constraints
- 2≤N≤105
- 0≤xi,yi≤109
- All input values are integers.
Input
Input is given from Standard Input in the following format:
N
x1 y1
x2 y2
:
xN yN
Output
Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.
Sample Input 1
3
1 5
3 9
7 8
Sample Output 1
3
Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.
Sample Input 2
6
8 3
4 9
12 19
18 1
13 5
7 6
Sample Output 2
8
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
#define nu 1000005
#define maxnum 100005
#define num 30
int n;
struct Md
{
int v,u,flag; }M[maxnum],C[maxnum];
struct Node
{
int x,y,z; }dis[nu];
bool cmp(Md a,Md b){
return a.v<b.v;
}
bool cmp0(Md a,Md b){
return a.u<b.u;
}
bool cmp1(Node a,Node b){
return a.z<b.z;
}
int book[maxnum];
void init()
{
for(int i=1;i<=n;i++)
book[i]=i; }
int getx(int x)
{
if(book[x]!=x)
book[x]=getx(book[x]);
return book[x];
}
void mergexy(int x,int y)
{
book[y]=x;
}
void change(){
for(int i=1;i<=n;i++)
{
C[i].v=M[i].v;
C[i].u=M[i].u;
C[i].flag=M[i].flag;
}
}
int main()
{
scanf("%d",&n); for(int i=1;i<=n;i++){
scanf("%d%d",&M[i].v,&M[i].u);
M[i].flag=i;
}
/* int t=unique(M+1,M+n)-M; n=t;*/
change();
int fl=0;
sort(C+1,C+n+1,cmp);
for(int i=1;i<n;i++)
{
dis[++fl].x=C[i].flag,dis[fl].y=C[i+1].flag,dis[fl].z=(min(fabs(C[i].v-C[i+1].v),fabs(C[i].u-C[i+1].u)));
}
change();
sort(C+1,C+n+1,cmp0);
for(int i=1;i<n;i++)
{
dis[++fl].x=C[i].flag,dis[fl].y=C[i+1].flag,dis[fl].z=(min(fabs(C[i].v-C[i+1].v),fabs(C[i].u-C[i+1].u)));
}
sort(dis+1,dis+fl+1,cmp1);
/* cout<<flag<<endl;
for(int i=1;i<=flag;i++)
cout<<dis[i].z<<endl;*/
init();
int number=0;
int sum=0;
for(int i=1;i<=fl;i++)
{
int p=getx(dis[i].x),q=getx(dis[i].y);
if(p!=q){
mergexy(p,q);
number++;
sum+=dis[i].z;
}
if(number==n) break;
}
cout<<sum<<endl;
return 0;
}
Built(最小生成树+构图离散化)的更多相关文章
- ABC065D Built[最小生成树]
这题和某道最短路题神似.对于任意点对,将他们连边,不如将他们分别沿$x,y$轴方向上点按顺序连起来,这样不仅可能多连通一些点,也花费更低,所以按照最短路那题的连边方式跑一个kruskal就行了. #i ...
- POJ2528Mayor's posters[线段树 离散化]
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59683 Accepted: 17296 ...
- POJ 2528 Mayor's posters(线段树/区间更新 离散化)
题目链接: 传送门 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of By ...
- 南阳理工 题目9:posters(离散化+线段树)
posters 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 The citizens of Bytetown, AB, could not stand that ...
- Mayor's posters(线段树+离散化POJ2528)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59239 Accepted: 17157 ...
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ...
- POJ 2528 区间染色,求染色数目,离散化
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 47905 Accepted: 13903 ...
随机推荐
- replace()函数用法
replace()函数表示将用一个字符串替换字符串中的所出现的特定内容. 语法为:replace(字段1,字段2,字段3),意思为字段3将会替换字段1里与字段2相同的内容 列如: table1 st ...
- Java并发之synchronized深入
一句话总结synchronized: JVM会自动通过使用monitor来加锁和解锁,保证了同时只有一个线程可以执行指定代码,从而保证了线程安全,同时具有可重入和不可中断的性质. 一.synchron ...
- Python在线教程(廖雪峰)
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000
- 设计模式--原型模式C++实现
原型模式C++实现 1定义 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象 2类图 3实现 class Prototype { protected: Prototype(); publ ...
- 12.详解Condition的await和signal等待通知机制
1.Condition简介 任何一个java对象都天然继承于Object类,在线程间实现通信的往往会应用到Object的几个方法,比如wait(),wait(long timeout),wait(lo ...
- 发送垃圾邮件的僵尸网络——药物(多)、赌博、股票债券等广告+钓鱼邮件、恶意下载链接、勒索软件+推广加密货币、垃圾股票、色情网站(带宏的office文件、pdf等附件)
卡巴斯基实验室<2017年Q2垃圾邮件与网络钓鱼分析报告> 米雪儿 2017-09-07 from:http://www.freebuf.com/articles/network/1465 ...
- C++复习4.函数设计基础
C/C++ 函数设计基础 20130918 函数式程序的基本功能单元,是模块化程序设计的基础,即使函数的功能正确是不够的,因为函数设计的细微缺点很容易导致函数被错用. 了解函数的基本知识,堆栈和堆的相 ...
- 内存保护机制及绕过方法——利用Ret2Libc绕过DEP之ZwSetInformationProcess函数
1. DEP内存保护机制 1.1 DEP工作原理 分析缓冲区溢出攻击,其根源在于现代计算机对数据和代码没有明确区分这一先天缺陷,就目前来看重新去设计计算机体系结构基本上是不可能的,我们只能靠 ...
- json 解析double类型多出几位小数,利用NSDecimalNumber修复 , 服务器返回的数据,用数据类型接收(int,NSNumber)?
后台返回float类型数据8.2,实际用AFN的json解析后得到8.1999999999,该怎么处理呢? 开发中处理处理价格金额问题, 后台经常返回float.double类型, 打印或转成NSS ...
- 对于get方法是否需要synchronized修饰
具体用法没有总结,只是说明一个用法而已,对于以前个人理解出现的偏差 [问题描述] 对于一个计数功能的实现,获取值的方法是否需要加锁? [以前理解] 我只需要在进行累加的方法上进行加锁即可,这样保证其可 ...