Object Clustering

Description

We have N (N ≤ 10000) objects, and wish to classify them into several groups by judgement of their resemblance. To simply the model, each object has 2 indexes a and b (a, b ≤ 500). The resemblance of object i and object j is defined by dij = |ai - aj| + |bi - bj|, and then we say i is dij resemble to j. Now we want to find the minimum value of X, so that we can classify the N objects into K (K < N) groups, and in each group, one object is at most X resemble to another object in the same group, i.e, for every object i, if i is not the only member of the group, then there exists one object j (i ≠ j) in the same group that satisfies dij ≤ X

Input

The first line contains two integers N and K. The following N lines each contain two integers a and b, which describe a object.

Output

A single line contains the minimum X.

Sample Input

6 2
1 2
2 3
2 2
3 4
4 3
3 1
Sample Output

2
Source

POJ Monthly–2007.08.05, Li, Haoyuan
题解

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<bitset>
#include<set>
#include<stack>
#include<map>
#include<list>
#include<new>
#include<vector>
#define MT(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pi=acos(-1.0);
const double E=2.718281828459;
const ll mod=1e8+7;
const int INF=0x3f3f3f3f;int n,k;struct node{int x;int y;int id;bool friend operator<(node a,node b){return a.x==b.x?a.y<b.y:a.x<b.x;///保证树状数组更新和查询时不会遗漏}
}point[10005];struct edge{int s;int e;int c;bool friend operator<(edge a,edge b){return a.c<b.c;}
}load[40000];
int sign=0;
int p[10005];
int find(int x){return p[x]==x?x:p[x]=find(p[x]);
}void kruskal(){for(int i=1;i<=n;i++)p[i]=i;sort(load+1,load+1+sign);int cnt=0;for(int i=1;i<=sign;i++){int x=find(load[i].s);int y=find(load[i].e);if(x!=y){cnt++;p[x]=y;if(cnt==n-k){printf("%d\n",load[i].c);return ;}}}
}int id[10005]; ///y-x为索引的编号
int xy[10005]; ///y-x为索引 x+y的最小值void update(int index,int minn,int s)   ///index:y-x  minn:x+y   s:编号
{index+=1000;for(int i=index;i>=1;i-=(i&(-i))){if(xy[i]>minn){xy[i]=minn;id[i]=s;}}
}void query(int index,int minn,int s)    ///index:y-x  minn:x+y   s:编号
{index+=1000;int e=-1,c=INF;///现在以编号s为原点,查询y-x>=index的点中x+y的最小值for(int i=index;i<10000;i+=(i&(-i))){if(xy[i]<c){e=id[i];c=xy[i];}}if(e!=-1)load[++sign]=edge{s,e,c-minn};
}void build_edge()
{/// 以(xi,yi)为原点,对于第1区域内的点(x,y)满足条件/// x>=xi,y-x>=yi-xi,(x+y)最小sort(point+1,point+1+n);memset(id,-1,sizeof(id));fill(xy,xy+10005,INF);///按照x升序///保证后面查询时,x都比当前的x大for(int i=n;i>=1;i--){int index=point[i].y-point[i].x;int minn=point[i].x+point[i].y;query(index,minn,point[i].id);update(index,minn,point[i].id);}
}int main()      ///第K大边
{scanf("%d %d",&n,&k);for(int i=1;i<=n;i++){scanf("%d %d",&point[i].x,&point[i].y);point[i].id=i;}///1象限建边build_edge();///2象限建边for(int i=1;i<=n;i++)swap(point[i].x,point[i].y);build_edge();///3象限建边for(int i=1;i<=n;i++)point[i].x=-point[i].x;build_edge();///4象限建边for(int i=1;i<=n;i++)swap(point[i].x,point[i].y);build_edge();kruskal();return 0;
}

POJ 3241Object Clustering曼哈顿距离最小生成树相关推荐

  1. 曼哈顿距离最小生成树莫队算法

    参考资料:https://www.cnblogs.com/CsOH/p/5904430.html https://blog.csdn.net/huzecong/article/details/8576 ...

  2. 曼哈顿距离最小生成树与莫队算法

    一.曼哈顿距离最小生成树 曼哈顿距离最小生成树问题可以简述如下: 给定二维平面上的N个点,在两点之间连边的代价为其曼哈顿距离,求使所有点连通的最小代价. 朴素的算法可以用O(N2)的Prim,或者处理 ...

  3. 曼哈顿距离最小生成树

    一.参考博客 博客:曼哈顿距离最小生成树与莫队算法 博客:学习总结:最小曼哈顿距离生成树 二.前置知识 1.曼哈顿距离:给定二维平面上的N个点,在两点之间连边的代价.(即distance(P1,P2) ...

  4. 曼哈顿距离最小生成树与莫队算法(总结)

    曼哈顿距离最小生成树与莫队算法(总结) 1 曼哈顿距离最小生成树 曼哈顿距离最小生成树问题可以简述如下:  给定二维平面上的N个点,在两点之间连边的代价为其曼哈顿距离,求使所有点连通的最小代价.  朴 ...

  5. 51nod 1213 二维曼哈顿距离最小生成树

    1213 二维曼哈顿距离最小生成树 基准时间限制:4 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 二维平面上有N个坐标为整数的点,点x1 y1同点x2 y2之间 ...

  6. 图论 —— 生成树 —— 曼哈顿距离最小生成树

    [概述] 当给出一些二维平面的点时,记两点间距离为曼哈顿距离,此时的最小生成树,称为曼哈顿最小距离生成树. 对于 n 个点来说,最朴素的做法是暴力求出所有所有点两两之间的曼哈顿距离,然后再跑最小生成树 ...

  7. 2018东北四省赛 Spin A Web 曼哈顿距离最小生成树

    莫队的论文,讲的很清晰 问题描述:给定平面N个点,两边相连的代价为曼哈顿距离,求这些点的最小生成树 按一般想法,prime复杂度O(n^2),Kruskal复杂度O(n^2 logn),N很大时,这复 ...

  8. 51nod 1213 二维曼哈顿距离最小生成树 树状数组+最小生成树

    Description 二维平面上有N个坐标为整数的点,点x1 y1同点x2 y2之间的距离为:横纵坐标的差的绝对值之和,即:Abs(x1 - x2) + Abs(y1 - y2)(也称曼哈顿距离). ...

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

    题目链接:http://poj.org/problem?id=3241 题意:平面上有n个点集,现在把他们分成k个集合,使得每个集合中的每个点都至少有一个本集合的点之间的曼哈顿距离不大于X,求最小的X ...

最新文章

  1. Catel(翻译)-为什么选择Catel
  2. Android的几个分析工具
  3. Spring boot 默认日志配置
  4. 彻底理解HashMap的元素插入原理
  5. 物联网ZigBee3.0协议E18-2G4U04B模块无线数据抓包调试的方法
  6. java延迟覆盖_高效Java第九条覆盖equals时总要覆盖hashCode
  7. java   cxf实现webservice接口方式之不依赖spring
  8. Hadoop框架:DataNode工作机制详解
  9. Http Header的Transfer-Encoding
  10. server接收dtu透传代码_Gopher2020大会干货总结:代码技巧篇
  11. Python 设计模式之建造者模式 Builder Pattern
  12. 代码证年审 年报 附文档 短消息类服务接入代码 电信业务资源综合管理系统用户手册-码号年报 (码号使用单位)
  13. 网卡是如何将包转换成电信号并发送到网线中的
  14. mysql数据库+查询+sequence_MySQL数据库InnoDB存储引擎Log漫游
  15. 光标移动事件。 gridview光标移动变色
  16. 国产化Demo(dm7+Tongweb7+java)
  17. wps office应用计算机等级考试,全国计算机等级考试一级教程:计算机基础及WPS Office应用(2016年版)...
  18. 用python 控制台打印图片示例
  19. openwrt 认证收费_openwrt,wifi认证-nodogsplash
  20. 90后薪资5年翻10倍,靠的不是钢琴手,而是真家伙!

热门文章

  1. ScheduledExecutorService
  2. 七边形简单画法步骤图_眼线笔的画法步骤图
  3. Maven项目错误解决小结
  4. 查看centos当前版本
  5. Android广播机制:Broadcast
  6. php实现解压功能的函数
  7. kvm连接服务器显示不全有重影,KVM延长器系列常见问题及解决方法
  8. mpandroidchart y轴从0开始_从零开始学Pytorch(十七)之目标检测基础
  9. arm linux 进程页表,linux-kernel – ARM Linux页表项格式 – 未使用的位?
  10. 单片机c语言数码显示实验报告,单片机动态显示技术实验报告.doc