POJ3241--Object Clustering(曼哈顿最小生成树)
2014-09-04 18:36
309 查看
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
Sample Output
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove二维平面中有一些点,两点之间的距离为曼哈顿距离,求最小生成树。朴素的n个点,只能做到O(n^3)或者O(n^2 lgn)。但是针对这种曼哈顿距离的MST。其中有个性质是:对于某个点,以他为中心的区域分为8个象限,对于每一个象限,只会取距离最近的一个点连边。这样的话,由于边是双向的,所以对于每个点只需要考虑4个方向,边的数目最多为O(4*n),再使用kruskal就可以做到O(nlgn)了。至于证明:这个结论可以证明如下:假设我们以点A为原点建系,考虑在y轴向右45度区域内的任意两点B(x1,y1)和C(x2,y2),不妨设|AB|≤|AC|(这里的距离为曼哈顿距离),如下图:
|AB|=x1+y1,|AC|=x2+y2,|BC|=|x1-x2|+|y1-y2|。而由于B和C都在y轴向右45度的区域内,有y-x>0且x>0。下面我们分情况讨论:1. x1>x2且y1>y2。这与|AB|≤|AC|矛盾;2. x1≤x2且y1>y2。此时|BC|=x2-x1+y1-y2,|AC|-|BC|=x2+y2-x2+x1-y1+y2=x1-y1+2*y2。由前面各种关系可得y1>y2>x2>x1。假设|AC|<|BC|即y1>2*y2+x1,那么|AB|=x1+y1>2*x1+2*y2,|AC|=x2+y2<2*y2<|AB|与前提矛盾,故|AC|≥|BC|;3. x1>x2且y1≤y2。与2同理;4. x1≤x2且y1≤y2。此时显然有|AB|+|BC|=|AC|,即有|AC|>|BC|。综上有|AC|≥|BC|,也即在这个区域内只需选择距离A最近的点向A连边。转自:http://blog.csdn.net/huzecong/article/details/8576908 那么如果有了结论之后,怎么样筛选出每个区域最近的点首先是8个方向,由于边的双向性,我们只需要针对一个点考虑4个方向即可。这4个方向(比如说Y轴右侧),那么都可以通过坐标变换到某一个区域,(比如说y>x)。关于y=x或者y=0对称就可以实现。对于y>x这个区域,如果对于点A(x0,y0) 在这个区域中有个点B(x1,y1)。那么x1>x0&&y1-x1>y0-x0。而dist(A,B)=x1-x0+y1-y0=x1+y1-(x0+y0),那么对于点A,则是找在这个区域内x1+y1最小的点。那么什么样的点满足在这个区域内呢,(X1>X0&&Y1-X1>Y0-X0)便 是条件我们将坐标按X排序,将Y-X离散化,用BIT来维护,查询对于某一个X0,查询比(Y0-X0)大的中X1+Y1最小的点。将这条边加上,重复4个区域之后,就是kruskal了。
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
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove二维平面中有一些点,两点之间的距离为曼哈顿距离,求最小生成树。朴素的n个点,只能做到O(n^3)或者O(n^2 lgn)。但是针对这种曼哈顿距离的MST。其中有个性质是:对于某个点,以他为中心的区域分为8个象限,对于每一个象限,只会取距离最近的一个点连边。这样的话,由于边是双向的,所以对于每个点只需要考虑4个方向,边的数目最多为O(4*n),再使用kruskal就可以做到O(nlgn)了。至于证明:这个结论可以证明如下:假设我们以点A为原点建系,考虑在y轴向右45度区域内的任意两点B(x1,y1)和C(x2,y2),不妨设|AB|≤|AC|(这里的距离为曼哈顿距离),如下图:
|AB|=x1+y1,|AC|=x2+y2,|BC|=|x1-x2|+|y1-y2|。而由于B和C都在y轴向右45度的区域内,有y-x>0且x>0。下面我们分情况讨论:1. x1>x2且y1>y2。这与|AB|≤|AC|矛盾;2. x1≤x2且y1>y2。此时|BC|=x2-x1+y1-y2,|AC|-|BC|=x2+y2-x2+x1-y1+y2=x1-y1+2*y2。由前面各种关系可得y1>y2>x2>x1。假设|AC|<|BC|即y1>2*y2+x1,那么|AB|=x1+y1>2*x1+2*y2,|AC|=x2+y2<2*y2<|AB|与前提矛盾,故|AC|≥|BC|;3. x1>x2且y1≤y2。与2同理;4. x1≤x2且y1≤y2。此时显然有|AB|+|BC|=|AC|,即有|AC|>|BC|。综上有|AC|≥|BC|,也即在这个区域内只需选择距离A最近的点向A连边。转自:http://blog.csdn.net/huzecong/article/details/8576908 那么如果有了结论之后,怎么样筛选出每个区域最近的点首先是8个方向,由于边的双向性,我们只需要针对一个点考虑4个方向即可。这4个方向(比如说Y轴右侧),那么都可以通过坐标变换到某一个区域,(比如说y>x)。关于y=x或者y=0对称就可以实现。对于y>x这个区域,如果对于点A(x0,y0) 在这个区域中有个点B(x1,y1)。那么x1>x0&&y1-x1>y0-x0。而dist(A,B)=x1-x0+y1-y0=x1+y1-(x0+y0),那么对于点A,则是找在这个区域内x1+y1最小的点。那么什么样的点满足在这个区域内呢,(X1>X0&&Y1-X1>Y0-X0)便 是条件我们将坐标按X排序,将Y-X离散化,用BIT来维护,查询对于某一个X0,查询比(Y0-X0)大的中X1+Y1最小的点。将这条边加上,重复4个区域之后,就是kruskal了。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define maxn 100005 int lowbit(int x) { return x & (-x); } struct Point { int x,y,id; bool operator < (const Point p)const { return x != p.x ? x<p.x:y<p.y; } }p[maxn]; struct BIT { int min_val,pos; void init() { min_val = (1<<30); pos = -1; } }bit[maxn]; struct Edge { int u,v,d; bool operator < (const Edge e)const { return d < e.d; } }e[maxn<<2]; int n,tot,father[maxn]; int find(int x) { if(father[x] == x) return x; return father[x] = find(father[x]); } int dist(int i,int j) { return abs(p[i].x - p[j].x) + abs(p[i].y - p[j].y); } void addedge(int u,int v,int d) { e[tot].u = u; e[tot].v = v; e[tot++].d = d; } void update(int x,int val,int pos) { for(int i = x;i >= 1;i -= lowbit(i)) { if(val < bit[i].min_val) bit[i].min_val = val,bit[i].pos = pos; } } int ask(int x,int m) { int min_val = (1<<30),pos = -1; for(int i = x;i <= m;i += lowbit(i)) { if(bit[i].min_val < min_val) min_val = bit[i].min_val,pos = bit[i].pos; } return pos; } int k; int a[maxn],b[maxn]; int Manhattan_minmum_spanning_tree(int n,Point *p) { for(int dir = 0;dir < 4;dir++) { if(dir == 1 || dir == 3) { for(int i = 0;i < n;i++) swap(p[i].x,p[i].y); } else if(dir == 2) { for(int i = 0;i < n;i++) p[i].x = -p[i].x; } sort(p,p+n); for(int i = 0;i < n;i++) { a[i] = b[i] = p[i].y - p[i].x; } sort(b,b+n); int m = unique(b,b+n)-b; for(int i = 1;i <= m;i++) bit[i].init(); for(int i = n-1;i >= 0;i--) { int pos = lower_bound(b,b+m,a[i])-b+1; int ans = ask(pos,m); if(ans != -1) addedge(p[i].id,p[ans].id,dist(i,ans)); update(pos,p[i].x+p[i].y,i); } } sort(e,e+tot); int cnt = n-k; for(int i = 0;i < n;i++) father[i] = i; for(int i = 0;i < tot;i++) { int u = e[i].u,v = e[i].v; int fa = find(u),fb = find(v); if(fa != fb) { cnt--; father[fa] = fb; if(cnt == 0) return e[i].d; } } } int main() { while(scanf("%d%d",&n,&k)==2 && n) { tot = 0; for(int i = 0;i < n;i++) { scanf("%d%d",&p[i].x,&p[i].y); p[i].id = i; } printf("%d\n",Manhattan_minmum_spanning_tree(n,p)); } return 0; }
相关文章推荐
- POJ3241 Object Clustering 曼哈顿最小生成树
- Object Clustering - POJ 3241 曼哈顿最小生成树
- poj 3241 Object Clustering 曼哈顿最小生成树
- POJ 3241 Object Clustering 曼哈顿距离求最小生成树的第K小边
- poj 3241 Object Clustering 莫队算法,曼哈顿最小生成树
- poj 3241 Object Clustering(曼哈顿最小生成树)
- POJ 3241 Object Clustering 曼哈顿最小生成树
- POJ 3241 Object Clustering 平面曼哈顿最小生成树
- 【POJ 3241】Object Clustering 曼哈顿距离最小生成树
- POJ 3241 Object Clustering 二维平面曼哈顿距离最小生成树
- poj 3241 Object Clustering (曼哈顿距离最小生成树)
- poj 3241 Object Clustering 曼哈顿最小生成树
- POJ 3241 Object Clustering 曼哈顿距离最小生成树
- 平面点曼哈顿最小生成树——POJ 3241 Object Clustering
- POJ 3241 Object Clustering (曼哈顿距离最小生成树)
- POJ - 3241 Object Clustering(莫队算法/曼哈顿最小生成树)
- [曼哈顿距离最小生成树 模板题] POJ 3241 Object Clustering
- POJ-3241 Object Clustering 曼哈顿最小生成树
- POJ 3214 Object Clustering 哈夫曼距离最小生成树
- poj3241 曼哈顿最小距离生成树第k大的边