hdu 4347 The Closest M Points(kd树+优先队列)

The Closest M Points

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)
Total Submission(s): 2338    Accepted Submission(s): 922


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
 

Author
HIT
 

Source
 题目大意:求取一个点的m个最近的点
题目分析:kd树+优先队列 模板题
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cstdio>
#define MAX (55555)
#define K (5)
#define INF (0x3f3f3f3f)
#define pow(x) ((x)*(x))

using namespace std;

int k,n,idx;

struct Point
{
    int x[K];
    bool operator < ( const Point &u ) const 
    {
        return x[idx] < u.x[idx];
    }
}po[MAX];

typedef pair<double,Point> PDP;
priority_queue<PDP> nq;

struct Tree
{
    Point p[MAX<<2];
    int son[MAX<<2];

    void build ( int l , int r , int u = 1 , int dep = 0 )
    {
        if ( l > r ) return;
        son[u] = r-l;
        son[u<<1] = son[u<<1|1] = -1;
        idx = dep%k;
        int mid = l + r >> 1;
        nth_element ( po+l , po+mid , po+r+1 );
        p[u] = po[mid];
        build ( l , mid-1 , u<<1 , dep+1 );
        build ( mid+1 , r , u<<1|1 , dep+1 );
    }

    void query ( Point a , int m , int u = 1 , int dep = 0 )
    {
        if ( son[u] == -1 ) return;
        PDP nd ( 0 , p[u] );
        for ( int i = 0 ; i < k ; i++ )
            nd.first += pow ( nd.second.x[i] - a.x[i] );
        int dim = dep%k , fg = 0;
        int x = u<<1 , y = u<<1|1;
        if ( a.x[dim] >= p[u].x[dim] ) swap ( x , y );
        if ( ~son[x] ) query ( a , m , x , dep+1 );
        if ( nq.size() < m ) nq.push ( nd ) , fg = 1;
        else
        {
            if ( nd.first < nq.top().first ) nq.pop() , nq.push ( nd );
            if ( pow ( a.x[dim] - p[u].x[dim] ) < nq.top().first ) fg = 1;
        }
        if (~son[y]&&fg ) query ( a , m , y , dep+1 );
    }

}kd;

void print ( Point &a )
{
    for ( int j = 0 ; j < k ; j++ )
        printf ( "%d%c" , a.x[j] , j==k-1?'\n':' ' );
}

int main ( )
{
    while ( ~scanf ( "%d%d" , &n , &k ) )
    {
        for ( int i = 0 ; i < n ; i++ )
            for ( int j = 0 ; j < k ; j++ )
                scanf ( "%d" , &po[i].x[j] );
        kd.build ( 0 , n-1 );
        int t,m;
        scanf ( "%d" , &t );
        while ( t-- )
        {
            Point ans;
            for ( int j = 0 ; j < k ; j++ ) 
                scanf ( "%d" , &ans.x[j] );
            scanf ( "%d" , &m );
            kd.query ( ans , m );
            printf ( "the closest %d points are:\n" , m );
            Point pt[20];
            for ( int j = 0 ; !nq.empty() ; j++ )
                pt[j] = nq.top().second,
                nq.pop ( );
            for ( int j = m-1 ; j >= 0 ; j-- )
                print ( pt[j] );
        }
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值