日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/15 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3241Object Clustering曼哈顿距离最小生成树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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曼哈顿距离最小生成树的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。