POJ3241 Object Clustering 曼哈顿最小生成树
题意:转换一下就是求曼哈顿最小生成树的第n-k条边
参考:莫涛大神的论文《平面点曼哈顿最小生成树》
/*
Problem: 3241 User: 96655
Memory: 920K Time: 94MS
Language: C++ Result: Accepted
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn=;
const int INF=0x3f3f3f3f;
const int maxm=;
int n,k,p;
struct Point
{
int x,y,id;
bool operator<(const Point &e)const
{
if(x==e.x)return y<e.y;
return x<e.x;
}
} point[maxn];
struct Edge
{
int u,v,w;
bool operator<(const Edge &e)const
{
return w<e.w;
}
} edge[maxn*];
void addedge(int a,int b,int l,int r)
{
++p;
edge[p].u=a;
edge[p].v=b;
edge[p].w=abs(l-r);
}
struct Node
{
int len,id;
} node[(maxm+)<<];
void pushup(int rt)
{
node[rt].len=min(node[rt*].len,node[rt*+].len);
if(node[rt].len==node[rt*].len)node[rt].id=node[rt*].id;
else node[rt].id=node[rt*+].id;
}
void change(int rt,int l,int r,Point tt)
{
if(l==r)
{
node[rt].len=tt.x+tt.y;
node[rt].id=tt.id;
return;
}
int pos=tt.y-tt.x+;
int m=(l+r)>>;
if(pos<=m)change(rt*,l,m,tt);
else change(rt*+,m+,r,tt);
pushup(rt);
}
Node query(int rt,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
return node[rt];
int m=(l+r)>>;
if(y<=m)return query(rt*,l,m,x,y);
else if(x>m)return query(rt*+,m+,r,x,y);
else
{
Node t1,t2;
t1=query(rt*,l,m,x,y);
t2=query(rt*+,m+,r,x,y);
if(t1.len<t2.len)return t1;
else return t2;
}
}
int fa[maxn];
int find(int x)
{
if(x==fa[x])return x;
return fa[x]=find(fa[x]);
}
void init()
{
for(int i=; i<(maxm<<); ++i)
node[i].len=INF,node[i].id=-;
}
void build()
{
sort(point+,point++n);
init();
for(int i=n; i>; --i)
{
int s=point[i].x+point[i].y;
Node t=query(,,maxm,point[i].y-point[i].x+,maxm);
if(t.id!=-)
addedge(point[i].id,t.id,s,t.len);
change(,,maxm,point[i]);
} }
int solve()
{
sort(edge+,edge+p+);
int cnt=;
for(int i=; i<=p; ++i)
{
int fx=find(edge[i].u);
int fy=find(edge[i].v);
if(fx!=fy)
{
fa[fy]=fx;
++cnt;
if(cnt==n-k)return edge[i].w;
}
}
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=; i<=n; ++i)
scanf("%d%d",&point[i].x,&point[i].y),point[i].id=i;
for(int i=; i<=n; ++i)
fa[i]=i;
p=;
build();
for(int i=; i<=n; ++i)
point[i].y=-point[i].y;
build();
for(int i=; i<=n; ++i)
point[i].y=-point[i].y,swap(point[i].x,point[i].y);
build();
for(int i=; i<=n; ++i)
point[i].y=-point[i].y;
build();
printf("%d\n",solve());
return ;
}
POJ3241 Object Clustering 曼哈顿最小生成树的更多相关文章
- POJ 3241 Object Clustering 曼哈顿最小生成树
Object Clustering Description We have N (N ≤ 10000) objects, and wish to classify them into severa ...
- POJ3241 Object Clustering(最小生成树)题解
题意:求最小生成树第K大的边权值 思路: 如果暴力加边再用Kruskal,边太多会超时.这里用一个算法来减少有效边的加入. 边权值为点间曼哈顿距离,那么每个点的有效加边选择应该是和他最近的4个象限方向 ...
- 【POJ 3241】Object Clustering 曼哈顿距离最小生成树
http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...
- 【Poj3241】Object Clustering
Position: http://poj.org/problem?id=3241 List Poj3241 Object Clustering List Description Knowledge S ...
- poj 3241 Object Clustering (曼哈顿最小生成树)
Object Clustering Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 2640 Accepted: 806 ...
- 【poj3241】 Object Clustering
http://poj.org/problem?id=3241 (题目链接) MD被坑了,看到博客里面说莫队要写曼哈顿最小生成树,我就写了一个下午..结果根本没什么关系.不过还是把博客写了吧. 转自:h ...
- POJ 3241Object Clustering曼哈顿距离最小生成树
Object Clustering Description We have N (N ≤ 10000) objects, and wish to classify them into several ...
- 【BZOJ-2177】曼哈顿最小生成树 Kruskal + 树状数组
2177: 曼哈顿最小生成树 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 190 Solved: 77[Submit][Status][Discu ...
- BZOJ 2177: 曼哈顿最小生成树
Sol 考了好几次曼哈顿最小生成树,然而一直不会打...这次终于打出来了...神tm调试了2h...好蛋疼... 首先曼哈顿最小生成树有个结论就是讲它每45度分出一个象限,对于每个点,只与每个象限中离 ...
随机推荐
- 【BZOJ [1878】[SDOI2009]HH的项链
Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变 ...
- 使用自定义任务审批字段创建 SharePoint 顺序工作流
http://msdn.microsoft.com/zh-cn/library/hh824675(v=office.14).aspx#odc_sp14_ta_CreatingSPSeqWorkflow ...
- 在Hadoop分布式文件系统的索引和搜索
FROM:http://www.drdobbs.com/parallel/indexing-and-searching-on-a-hadoop-distr/226300241?pgno=3 在今天的信 ...
- 【贪心】bzoj 3709:[PA2014]Bohater
3709: [PA2014]Bohater Time Limit: 5 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 653 Solved: ...
- uva 10404
dp 1表示先手赢 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- 详细解析: VictorOps 是如何利用和完善 ChatOps?
ChatOps,即聊天应用,在软件开发中被广泛应用改进开发者之间的沟通.简单地说,ChatOps 是将内容或行动 (或两者) 迁移到聊天客户端.这样做之后,企业内的所有团队都能分享重要信息,行动,及其 ...
- ****php redis 的使用方法
phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系很有用;以下是redis官方提供的命令使用技巧:下载地址如下:https://github.com/owlie ...
- 使用头文件climits中的符号常量获知整型数据的表数范围---gyy整理
在头文件climits(limits.h)以宏定义的方式定义了各种符号常量来表示各种整型类型表示数的范围,如int的最大最小值,long的最大最小值等. 符号常量 表示 CHAR_BIT char 的 ...
- IOS - DatePicker的使用
UIDatePicker *picker = [[UIDatePicker alloc] init]; picker.datePickerMode = UIDatePickerModeDate; pi ...
- 传智博客(JavaWeb方面的所有知识)听课记录(经典)
一. JavaWeb基础 第一天: 1.Eclipse详解: (1).Bad versionnumber in .class file:编译器版本和运行(JRE)版本不符合.高的JRE版本 ...