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

来源:互联网 发布:java定义可变长度数组 编辑:程序博客网 时间:2024/05/22 04:36

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 = (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
 

Author
HIT
 

Source
2012 Multi-University Training Contest 5
 题目大意:求取一个点的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 0
原创粉丝点击