HDU 5992 Finding Hotels (kd tree)

Source: Internet
Author: User

Test instructions: N hotels, each hotel has coordinates x, Y, price per night z,m guests, coordinates x, Y, Money C, ask you each guest recently and can live in (not to spend the least money) of the hotel, as close to the top of the election.

Idea: kd Tree template problem

Code:

#include <Set>#include<map>#include<stack>#include<cmath>#include<queue>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>typedefLong Longll;Const intMAXN =200000+Ten;Const intSeed =131;Constll MOD = 1e9 +7;Const intINF =0x3f3f3f3f;using namespacestd;#defineLson RT << 1#defineRson RT << 1 | 1#definePair Pair<ll, node>intK, IDX;//number of dimensions Kstructnode{intfeature[3];//defining an array of attributes    intID; BOOL operator< (ConstNode &u)Const    {        returnFEATURE[IDX] <U.feature[idx];   }}_DATA[MAXN], p; //_data[] Array represents the input dataNode data[4* MAXN];//data[] Array represents all node data for the k-d treeintflag[4* MAXN];//used to mark whether a node exists, 1 is present, and 1 indicates that it does not existpriority_queue<Pair> Q;//Queue q is used to store the nearest m data from Pll Sqrt (ll A, ll b) {//Euclidean distance squared    return(A-B) * 1LL * (A-b);}//Achievement Step, Parameter dept The depth of the treevoidBuild (intLintRintRtintdept) {    if(L > R)return; FLAG[RT]=1;//indicates that a node with the number RT is presentFlag[lson] = Flag[rson] =-1;//child temporarily marked for current node does not existIDX = dept% K;//divide by attributes that are numbered IDX    intMid = (L + r) >>1; Nth_element (_data+ L, _data + Mid, _data + R +1);//nth_element () is a function in the STL algorithmDATA[RT] =_data[mid]; Build (L, Mid-1, Lson, Dept +1);//Recursive left subtreeBuild (Mid +1, R, Rson, dept +1);//Recursive right sub-tree}//query function to find the nearest M feature attribute from PvoidQuery (Node p,intMintRtintdept) {    if(Flag[rt] = =-1)return;//non-existent nodes do not traversePair cur (0, Data[rt]);//gets the data of the current node and the distance to P     for(inti =0; I < K; i++)//the square of Euclidean distanceCur.first + =Sqrt (LL) Data[rt].feature[i], (LL) p.feature[i]); intDim = Dept% K;//As with achievements, this ensures that the dim value of the same node will not change    BOOLFG =0;//used to mark whether the right subtree needs to be traversed    intx =Lson; inty =Rson; if(P.feature[dim] >= Data[rt].feature[dim])//The Dim characteristic value of the data p is greater than or equal to the current data, you need to enter the right subtreeswap (x, y); if(~flag[x]) Query (P, M, X, dept +1);//if node x exists, then go to subtree to continue traversing//The following is a backtracking process that maintains a priority queue    if(Q.size () < m)//if the queue is not full, continue to put    {        if(cur.second.feature[2] <= p.feature[2]) Q.push (cur); FG=1; }    Else    {        if(Cur.first < Q.top (). First && cur.second.feature[2] <= p.feature[2])//If a smaller distance is found, the data that is used to replace the maximum distance in the queue Q{q.pop ();        Q.push (cur); }        Else if(Cur.first = = Q.top (). First && Cur.second.id < Q.top (). Second.id && cur.second.feature[2] <= p.feature[2]) {q.pop ();        Q.push (cur); }        if(Sqrt (LL) P.feature[dim], (LL) Data[rt].feature[dim]) <Q.top (). First) {FG=1; }    }    if(~flag[y] &&FG) Query (P, M, Y, dept+1);}intMain () {intT, N, M; K=2; scanf ("%d", &T);  while(t--) {scanf ("%d%d", &n, &m);  for(inti =0; I < n; i++){             for(intj =0; J <3; J + +) scanf ("%d", &_data[i].feature[j]); _data[i].id=i; } Build (0N1,1,0);  while(m--){             while(!Q.empty ())            Q.pop ();  for(inti =0; I <3; i++) scanf ("%d", &P.feature[i]); Query (P,1,1,0); P=Q.top (). Second; printf ("%d%d%d\n", p.feature[0], p.feature[1], p.feature[2]); }    }    return 0;}

Template:

#defineLson RT << 1#defineRson RT << 1 | 1#definePair Pair<ll, node>intK, IDX;//number of dimensions Kstructnode{intfeature[3];//defining an array of attributes    intID; BOOL operator< (ConstNode &u)Const    {        returnFEATURE[IDX] <U.feature[idx];   }}_DATA[MAXN], p; //_data[] Array represents the input dataNode data[4* MAXN];//data[] Array represents all node data for the k-d treeintflag[4* MAXN];//used to mark whether a node exists, 1 is present, and 1 indicates that it does not existpriority_queue<Pair> Q;//Queue q is used to store the nearest m data from Pll Sqrt (ll A, ll b) {//Euclidean distance squared    return(A-B) * 1LL * (A-b);}//Achievement Step, Parameter dept The depth of the treevoidBuild (intLintRintRtintdept) {    if(L > R)return; FLAG[RT]=1;//indicates that a node with the number RT is presentFlag[lson] = Flag[rson] =-1;//child temporarily marked for current node does not existIDX = dept% K;//divide by attributes that are numbered IDX    intMid = (L + r) >>1; Nth_element (_data+ L, _data + Mid, _data + R +1);//nth_element () is a function in the STL algorithmDATA[RT] =_data[mid]; Build (L, Mid-1, Lson, Dept +1);//Recursive left subtreeBuild (Mid +1, R, Rson, dept +1);//Recursive right sub-tree}//query function to find the nearest M feature attribute from PvoidQuery (Node p,intMintRtintdept) {    if(Flag[rt] = =-1)return;//non-existent nodes do not traversePair cur (0, Data[rt]);//gets the data of the current node and the distance to P     for(inti =0; I < K; i++)//the square of Euclidean distanceCur.first + =Sqrt (LL) Data[rt].feature[i], (LL) p.feature[i]); intDim = Dept% K;//As with achievements, this ensures that the dim value of the same node will not change    BOOLFG =0;//used to mark whether the right subtree needs to be traversed    intx =Lson; inty =Rson; if(P.feature[dim] >= Data[rt].feature[dim])//The Dim characteristic value of the data p is greater than or equal to the current data, you need to enter the right subtreeswap (x, y); if(~flag[x]) Query (P, M, X, dept +1);//if node x exists, then go to subtree to continue traversing    if(Q.size () < m)//if the queue is not full, continue to put{//Note that the fg=1 must be made in the future.Q.push (cur); FG=1; }    Else    {        if(Cur.first < Q.top (). First)//If a smaller distance is found, the data that is used to replace the maximum distance in the queue Q{q.pop ();        Q.push (cur); }        if(Sqrt (LL) P.feature[dim], (LL) Data[rt].feature[dim]) <Q.top (). First) {FG=1; }    }    if(~flag[y] &&FG) Query (P, M, Y, dept+1);}

HDU 5992 Finding Hotels (kd tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.