1. 程式人生 > >【POJ3714】Raid:平面最近點對

【POJ3714】Raid:平面最近點對

Description

After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union's attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N

 nuclear power stations and breaking down any of them would disable the system.

The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?

Input

The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 100000).
The next N lines describe the positions of the stations. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y

 (0 ≤ Y ≤ 1000000000) indicating the positions of the station.
The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent.  

Output

For each test case output the minimum distance with precision of three decimal placed in a separate line.

Sample Input

2
4
0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3
4
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

Sample Output

1.414
0.000

解析
第一次做平面最近點對的題,就遇到了一道變種......
最接近點對問題的提法是:給定平面上n個點,找其中的一對點,使得在n個點的所有點對中,該點對的距離最小。
但是在Raid這道題中,要求兩個集合S1和S2中的最近點對,按理說程式碼應該更加複雜。
然而,這裡找到了一個更簡單的做法。
程式碼如下:
#include<cstring>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int T,n;
struct node{
    double x,y;
}e[100055],d[100045];
bool cmp(node a,node b)
{
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
double dis(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        double ans=1e11;
        scanf("%d",&n);
        for(register int i=1;i<=n;++i)
        {
            scanf("%lf%lf",&e[i].x,&e[i].y);
        }
        for(register int i=1;i<=n;++i)
        {
            scanf("%lf%lf",&d[i].x,&d[i].y);
        }
        sort(e+1,e+n+1,cmp);
        sort(d+1,d+n+1,cmp);
        int t = 1;
        for(int i=1;i<=n;++i)
        {
            while(t<n&&cmp(e[t+1],d[i])) ++t;//以d[i]的橫座標為分界線,將e點分為兩部分 
            for(int j=t;j<=n;++j)//掃描{e}的第一部分 
            {
                if(fabs(e[j].x-d[i].x)>ans) break;
                ans= min(ans, dis(e[j],d[i]));
                //往下掃描的同時,將ans向小更新。很顯然,{e}經過排序之後,不需要幾次就會break; 
            }
            for(int j=t-1;j;--j)//掃描{e}的另一部分 
            {
                if(fabs(e[j].x-d[j].x)>ans) break;
                ans = min(ans,dis(e[j],d[i]));//同上 
            }
        }
        printf("%.3f\n",ans);
    }
    return 0;
}