POJ3241 Object Clustering(最小生成树)题解
题意:求最小生成树第K大的边权值
思路:
如果暴力加边再用Kruskal,边太多会超时。这里用一个算法来减少有效边的加入。
边权值为点间曼哈顿距离,那么每个点的有效加边选择应该是和他最近的4个象限方向的点。这里用一个树状数组维护以y-x为索引的y+x的值,然后这个数组所储存的就是一个点的第一象限方向的距离他最近的点。这样我们每次查找只要看(i,N)这个区间是否有一个点的距离比现在的更小(因为以y-x为索引,所以I>i就表示I这个点在i的第一象限方向)。最后每个方向的边都加完后,只要用Kruskal算法加边,加到第K就输出权值。
代码:
#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
const int N = 10000+5;
const int INIT = 1061109567;
using namespace std;
int n,k,cnt,Id[N],v[N],f[N];//id为v值的id序号,v为储存以y-x为索引的y+x值
struct node{
int x,y,id;
friend bool operator < (node a,node b){
return a.x == b.x? a.y < b.y : a.x < b.x;
}
}p[N];
struct edge{
int u,v,w;
friend bool operator < (edge a,edge b) {
return a.w < b.w;
}
}e[N<<2];
int lowbit(int x){
return x&(-x);
}
void query(int id,int pos,int val){
pos += 1000;
int u = -1,ret = INT_MAX;
for(int i = pos;i < N;i += lowbit(i)){
if(v[i] < ret && v[i] != INIT){ //找曼哈顿距离最短的
ret = v[i];
u = Id[i];
}
}
if(u != -1){ //找到这种点就加一个边
e[cnt].u = id;
e[cnt].v = u;
e[cnt++].w = ret - val;
}
}
void update(int id,int pos,int val){ //id,y-x,y+x
pos += 1000;
for(int i = pos;i > 0;i -= lowbit(i)){
if(val < v[i]){//更换最佳选择
v[i] = val;
Id[i] = id;
}
}
}
int find(int x){
if(x == f[x]) return x;
else return f[x] = find(f[x]);
}
void kruskal(){
sort(e,e+cnt);
for(int i = 0;i < N;i++) f[i] = i;
int num = n;
for(int i = 0;i < cnt;i++){
int x = find(e[i].u);
int y = find(e[i].v);
if(x != y){
f[x] = f[y];
num--;
if(num == k){
printf("%d\n",e[i].w);
return;
}
}
}
}
void addEdge(){
memset(v,63,sizeof(v)); //v == 1061109567
sort(p,p+n);
for(int i = n-1;i >= 0;i--){
int index = p[i].y - p[i].x;
int val = p[i].y + p[i].x;
query(p[i].id,index,val);
update(p[i].id,index,val);
}
}
int main(){
cnt = 0;
scanf("%d%d",&n,&k);
for(int i = 0;i < n ;i++){
scanf("%d%d",&p[i].x,&p[i].y);
p[i].id = i;
}
addEdge();
for(int i = 0;i < n;i++) swap(p[i].x,p[i].y);
addEdge();
for(int i = 0;i < n;i++) p[i].y = -p[i].y;
addEdge();
for(int i = 0;i < n;i++) swap(p[i].x,p[i].y);
addEdge();
kruskal();
return 0;
}
POJ3241 Object Clustering(最小生成树)题解的更多相关文章
- POJ3241 Object Clustering 曼哈顿最小生成树
题意:转换一下就是求曼哈顿最小生成树的第n-k条边 参考:莫涛大神的论文<平面点曼哈顿最小生成树> /* Problem: 3241 User: 96655 Memory: 920K Ti ...
- 【Poj3241】Object Clustering
Position: http://poj.org/problem?id=3241 List Poj3241 Object Clustering List Description Knowledge S ...
- POJ 3241 Object Clustering 曼哈顿最小生成树
Object Clustering Description We have N (N ≤ 10000) objects, and wish to classify them into severa ...
- 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 3241】Object Clustering 曼哈顿距离最小生成树
http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...
- POJ 3241 曼哈顿距离最小生成树 Object Clustering
先上几个资料: 百度文库有详细的分析和证明 cxlove的博客 TopCoder Algorithm Tutorials #include <cstdio> #include <cs ...
- POJ 3241 Object Clustering(Manhattan MST)
题目链接:http://poj.org/problem?id=3241 Description We have N (N ≤ 10000) objects, and wish to classify ...
- 老oj3444 && Pku3241 Object Clustering
Description We have N (N ≤ 10000) objects, and wish to classify them into several groups by judgemen ...
随机推荐
- LoadRunner-关联(自动关联和手动关联)
使用LR录制脚本,新建课程:每次新建课程都会产生一个新的courseid和partid,直接使用录制的脚本执行是不行的.所以用到了关联. 注:关联分手动和自动关联,自动关联搜索出一些不必要关联的数据, ...
- (转)Spring实现IoC的多种方式
原文地址:http://www.cnblogs.com/best/p/5727935.html 目录 一.使用XML配置的方式实现IOC 二.使用Spring注解配置IOC 三.自动装配 四.零配置实 ...
- 常用python包(依赖)Ubuntu下
amqp==1.4.9anyjson==0.3.3apturl==0.5.2beautifulsoup4==4.4.1billiard==3.3.0.23blinker==1.3Brlapi==0.6 ...
- post方式提交数据
<!DOCTYPE HTML><html> <head> <meta charset="utf-8" /> ...
- centos 基础修改文件权限
在centos 下 nginx 默认用户是user = apachegroup = apache 所以需要更改文件和文件夹权限时候需要满足apache用户才能进行 常用方式: $ chmod Runt ...
- qsv转mp4
1:下载格式工厂:http://rj.baidu.com/soft/detail/13052.html?ald 2:安装 :选择安装位置,把不需要安装的软件前面的对号去掉. 3:下一步,把不需要的软件 ...
- 机器学习理论基础学习13--- 隐马尔科夫模型 (HMM)
隐含马尔可夫模型并不是俄罗斯数学家马尔可夫发明的,而是美国数学家鲍姆提出的,隐含马尔可夫模型的训练方法(鲍姆-韦尔奇算法)也是以他名字命名的.隐含马尔可夫模型一直被认为是解决大多数自然语言处理问题最为 ...
- TempData["a"]多个Action方法之前共享数据
ViewData["a"]只可以在自己视图的页面里被访问,但TempData["a"]可以多个Action方法之前共享数据,比如在 @{Html.RenderA ...
- C语言进阶之路(三)----野指针的产生原因及解决办法
1.会产生野指针的做法 #include <stdio.h> //这就是一种错误的写法 int main(){ int *p = NULL; p = (); //释放P所指向的内存空间,但 ...
- 原生 ajax
1.创建XMLHttpRequest对象 var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Ope ...