poj 3241 Object Clustering 莫隊演算法,曼哈頓最小產生樹

來源:互聯網
上載者:User

Object Clustering

Time Limit: 2000MS   Memory Limit: 131072K
Total Submissions: 1732   Accepted: 432

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 21 22 32 23 44 33 1

Sample Output

2

Source POJ Monthly--2007.08.05, Li, Haoyuan 現成的東西,就不自己寫了。


轉載請註明出處,謝謝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了。


我的方法:原理用的是如上的。

實現的時候,也是計算y>x的地區的。但是樹狀數組以x-y離散化的結果排序的。

因為y'-x' > y-x

所以x'-y' < x-y

其實樹狀數組不僅可以實現首碼和,也可以實現尾碼和,沒必要反過來的。但是我就是習慣了


#include<iostream>#include<cstring>#include<cstdio>#include<math.h>#include<algorithm>#include<vector>using namespace std;#define maxn 10007#define inf 100000000#define maxm 2007int pos[maxm],tree[maxm];void add(int p,int val,int id){    while(p < maxm){        if(val < tree[p]){            tree[p] = val;            pos[p] = id;        }        p += p&(-p);    }}int query(int p){    int id=-1,val=inf;    while(p > 0){        if(tree[p] < val){            val = tree[p];            id = pos[p];        }        p -= p&(-p);    }    return id;}struct Node{    int x,y,id,xsy;};int comp(Node a,Node b){    if(a.x == b.x) return a.y > b.y;    return a.x > b.x;}int compxsy(Node a,Node b){    return a.xsy < b.xsy;}struct Edge{    int u,v,w;    Edge(int _u=0,int _v=0,int _w=0){        u =_u,v=_v,w=_w;    }};vector<Edge> edge;Node point[maxn];Node P[maxn];int dist(Node a,Node b){    return abs(a.x-b.x)+abs(a.y-b.y);}void ManhaTon(int n){    for(int i = 0;i < n; i++)        point[i].xsy = point[i].x-point[i].y;    sort(point,point+n,compxsy);    int cnt = 1,f=-inf;    for(int i = 0;i < n; i++){        if(point[i].xsy != f){            cnt++;            f = point[i].xsy;        }        point[i].xsy = cnt;    }    sort(point,point+n,comp);    for(int i = 0;i < maxm; i++)        tree[i] = inf,pos[i]=-1;    for(int i = 0;i < n; i++){        int u = point[i].id;        int v = query(point[i].xsy);        if(v != -1)            edge.push_back(Edge(u,v,dist(P[u],P[v])));        add(point[i].xsy,point[i].x+point[i].y,u);    }}void buildEdge(int n){    edge.clear();    for(int j = 0;j < 4; j++){        for(int i = 0;i < n; i++)            point[i] = P[i];        for(int i = 0;i < n; i++){            if(j == 1) swap(point[i].x,point[i].y);            else if(j == 2)                point[i].y = -point[i].y;            else if(j==3){                swap(point[i].x,point[i].y);                point[i].y = -point[i].y;            }        }        ManhaTon(n);    }}int pre[maxn];int find(int u){    if(u == pre[u]) return u;    return pre[u] = find(pre[u]);}int compEdge(Edge a,Edge b){    return a.w < b.w;}int MST(int n,int k){    for(int i = 0;i < n; i++)        pre[i] = i;    sort(edge.begin(),edge.end(),compEdge);    int d = 0;    for(int i = 0;i < edge.size() ;i++){        if(n == k) return d;        d = edge[i].w;        int f1 = find(edge[i].u);        int f2 = find(edge[i].v);        if(f1 == f2) continue;        pre[f1] = f2;        n--;    }    return d;}int main(){    int n,k;    while(scanf("%d%d",&n,&k)!=EOF){        for(int i = 0;i < n; i++){            scanf("%d%d",&P[i].x,&P[i].y);            P[i].id = i;        }        buildEdge(n);        printf("%d\n",MST(n,k));    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.