题意:求最小生成树第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(最小生成树)题解的更多相关文章

  1. POJ3241 Object Clustering 曼哈顿最小生成树

    题意:转换一下就是求曼哈顿最小生成树的第n-k条边 参考:莫涛大神的论文<平面点曼哈顿最小生成树> /* Problem: 3241 User: 96655 Memory: 920K Ti ...

  2. 【Poj3241】Object Clustering

    Position: http://poj.org/problem?id=3241 List Poj3241 Object Clustering List Description Knowledge S ...

  3. POJ 3241 Object Clustering 曼哈顿最小生成树

    Object Clustering   Description We have N (N ≤ 10000) objects, and wish to classify them into severa ...

  4. poj 3241 Object Clustering (曼哈顿最小生成树)

    Object Clustering Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 2640   Accepted: 806 ...

  5. 【poj3241】 Object Clustering

    http://poj.org/problem?id=3241 (题目链接) MD被坑了,看到博客里面说莫队要写曼哈顿最小生成树,我就写了一个下午..结果根本没什么关系.不过还是把博客写了吧. 转自:h ...

  6. 【POJ 3241】Object Clustering 曼哈顿距离最小生成树

    http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...

  7. POJ 3241 曼哈顿距离最小生成树 Object Clustering

    先上几个资料: 百度文库有详细的分析和证明 cxlove的博客 TopCoder Algorithm Tutorials #include <cstdio> #include <cs ...

  8. POJ 3241 Object Clustering(Manhattan MST)

    题目链接:http://poj.org/problem?id=3241 Description We have N (N ≤ 10000) objects, and wish to classify ...

  9. 老oj3444 && Pku3241 Object Clustering

    Description We have N (N ≤ 10000) objects, and wish to classify them into several groups by judgemen ...

随机推荐

  1. King's Quest---poj1904(连通图缩点)

    题目链接:http://poj.org/problem?id=1904 题意:国王有n个儿子,每个儿子喜欢ki个女孩,国王想让王子与他喜欢的人结婚,就让巫师做一个列表出来,但是国王想知道王子能和哪些女 ...

  2. grunt学习三-bower(二)

    一.通过bower help 来展开bower的命令 Usage: bower <command> [<args>] [<options>] Commands: c ...

  3. android 操作SD卡上的文件

    (1)说明:操作SD卡上的文件须要增加下面权限  在SD卡上创建和删除文件权限  <uses-permission android:name="android.permission.M ...

  4. C#静态类,静态构造函数,静态变量

    静态变量位于栈上,它是一个全局变量,在编译期就已经生成. public class Cow public static int count; private int id; { id = ++coun ...

  5. 五、Mosquitto 高级应用之权限管理

    本文将讲解 Mosquitto 权限管理.如果还没有搭建 Mosquitto 服务的可以参考我的另外两篇文章<< 一.Mosquitto 介绍&安装>> << ...

  6. 使用pssh进行并行批量操作

    假如同时给上千台服务器执行一个命令,拷贝一个文件,杀一个进程等,有什么简化运维管理的工具呢?在小型使用中我都是使用for循 环,数量巨大,一方面不确定操作是否成功,一方面for循环语句性能不好估计且是 ...

  7. CloudFlare Support - Error 522: Connection timed out 错误522:连接超时

    522错误意味着我们无法在所有到达原点Web服务器. 这方面有几个主要原因: 原始服务器太超载回应. 源Web服务器具有挡住了我们的请求的防火墙,或者数据包被主机的网络内下降. 源Web服务器脱机,或 ...

  8. #C++初学记录(算法4)

    A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...

  9. 模仿WIN32程序处理消息

    #include "stdafx.h" #include "MyMessage.h" #include <conio.h> using namesp ...

  10. Multinoulli distribution

    https://www.statlect.com/probability-distributions/multinoulli-distribution3 Multinoulli distributio ...