HDU4347--The Closest M Points(KD树)

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 = (p 1, p 2,..., p n) and q = (q 1, q 2,..., q n) 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 2 1 1 1 3 3 4 2 2 3 2 2 3 1
 

Sample Output
  
  
the closest 2 points are: 1 3 3 4 the 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 5
int 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值