HDU4347--The Closest M Points(KD树)

来源:互联网 发布:电子工资条软件 编辑:程序博客网 时间:2024/05/22 10:32
Problem Description
The course of Software Design and Development Practice is objectionable. ZLC is facing a serious problem .There are many points in K-dimensional space .Given a point. ZLC need to find out the closest m points. Euclidean distance is used as the distance metric between two points. The Euclidean distance between points p and q is the length of the line segment connecting them.In Cartesian coordinates, if p = (p1, p2,..., pn) and q = (q1, q2,..., qn) are two points in Euclidean n-space, then the distance from p to q, or from q to p is given by:

Can you help him solve this problem?
 

Input
In the first line of the text file .there are two non-negative integers n and K. They denote respectively: the number of points, 1 <= n <= 50000, and the number of Dimensions,1 <= K <= 5. In each of the following n lines there is written k integers, representing the coordinates of a point. This followed by a line with one positive integer t, representing the number of queries,1 <= t <=10000.each query contains two lines. The k integers in the first line represent the given point. In the second line, there is one integer m, the number of closest points you should find,1 <= m <=10. The absolute value of all the coordinates will not be more than 10000.
There are multiple test cases. Process to end of file.
 

Output
For each query, output m+1 lines:
The first line saying :”the closest m points are:” where m is the number of the points.
The following m lines representing m points ,in accordance with the order from near to far
It is guaranteed that the answer can only be formed in one ways. The distances from the given point to all the nearest m+1 points are different. That means input like this:
2 2
1 1
3 3
1
2 2
1
will not exist.
 

Sample Input
3 21 11 33 422 322 31
 

Sample Output
the closest 2 points are:1 33 4the closest 1 points are:1 3
题意:KD树模板题。
不懂KD树的可以先看这里:http://underthehood.blog.51cto.com/2531780/687160 
思路:M近邻和最近邻其实是一样的。M近邻只需要多个优先队列就行了。先一路递归到叶子节点,然后维护优先队列M个节点就OK。
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>using namespace std;#define LL long long int#define maxn 50080#define K 5int num,nownum,m;LL ans;struct kdNode{    LL x[K];     int div;    bool lef;}Ans[12];struct Node{    kdNode a;    LL dis;//表示和目标点的距离    bool operator < (const Node & a) const    {        return dis < a.dis;    }    Node(){}    Node(kdNode & tmp,LL d)    {        a = tmp;        dis = d;    }};int cmpNo;bool cmp(kdNode a,kdNode b){    return a.x[cmpNo] < b.x[cmpNo];}inline LL max(LL a,LL b){return a>b?a:b;}kdNode p[maxn],q;LL dis(kdNode a,kdNode b,int k){    LL res = 0;    for(int i = 0;i < k;i++)        res += (a.x[i] - b.x[i])*(a.x[i] - b.x[i]);    return res;}priority_queue <Node> qq;void buildKD(int l,int r,kdNode * p,int d,int k){    if(l > r)    return;    int m = (l+r) >> 1;    cmpNo = d;    nth_element(p+l,p+m,p+r+1,cmp);    p[m].div = d;if(l == r)    {p[m].lef = 1;return;}    buildKD(l,m-1,p,(d+1)%k,k);    buildKD(m+1,r,p,(d+1)%k,k);}void findKD(int l,int r,kdNode & tar,kdNode * p,int k){    if(l>r)    return;    int m = (l+r) >> 1;    LL d = dis(p[m],tar,k);    if(p[m].lef)//如果是叶子    {        if(nownum < num)            {            nownum++;            ans = max(ans,d);            qq.push(Node(p[m],d));        }        else if(ans > d)        {            qq.pop();            qq.push(Node(p[m],d));            ans = qq.top().dis;        }        return;    }    LL t = tar.x[p[m].div] - p[m].x[p[m].div];    if(t > 0)    {        findKD(m+1,r,tar,p,k);        if(nownum < num)        {            qq.push(Node(p[m],d));            nownum++;            ans = qq.top().dis;            findKD(l,m-1,tar,p,k);        }        else         {                if(ans > d)            {                qq.pop();                qq.push(Node(p[m],d));                ans = qq.top().dis;            }            if(ans > t*t)                findKD(l,m-1,tar,p,k);        }    }    else     {            findKD(l,m-1,tar,p,k);        if(nownum < num)        {            qq.push(Node(p[m],d));            nownum++;            ans = qq.top().dis;            findKD(m+1,r,tar,p,k);        }        else        {            if(ans > d)            {                qq.pop();                qq.push(Node(p[m],d));                ans = qq.top().dis;            }            if (ans > t*t)                findKD(m+1,r,tar,p,k);        }    }}int main(){    //freopen("in.txt","r",stdin);    int n,k;    while(scanf("%d%d",&n,&k)==2)    {        for(int i = 0;i < n;i++)        {            for(int j = 0;j < k;j++)            {                scanf("%I64d",&p[i].x[j]);            }            p[i].lef = 0;        }        int t;        scanf("%d",&t);        buildKD(0,n-1,p,k-1,k);        for(int i = 1;i <= t;i++)        {ans = -1;            nownum = 0;            for(int j = 0;j < k;j++)                scanf("%I64d",&q.x[j]);            while(!qq.empty())        qq.pop();            scanf("%d",&num);            findKD(0,n-1,q,p,k);            for(int j = 1;j <= num;j++)            {                Ans[j] = qq.top().a;                qq.pop();            }            printf("the closest %d points are:\n",num);            for(int j = num;j >= 1;j--)            {                for(int kk = 0;kk < k;kk++)                {                    if(kk == 0)                        printf("%I64d",Ans[j].x[kk]);                    else printf(" %I64d",Ans[j].x[kk]);                }                puts("");            }        }    }    return 0;}


0 0