您的位置:首页 > 其它

poj 3714(典型的最近点对问题)

2014-05-10 10:00 381 查看
Raid

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 8271Accepted: 2466
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

题目很容易理解就不多说了,上AC代码:


#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
#define MM 1e18;
struct Node{
LL x,y;
int s;
}a[200100],b[200100];    //注意这里数组大小,别忘了这里n的范围,结果runtime error了
int cmp1(Node t1,Node t2){
if(t1.x==t2.x)
return t1.y<t2.y;
return t1.x<t2.x;
}
int cmp2(Node t1,Node t2){
return t1.y<t2.y;
}
double dis(Node t1,Node t2){
if(t1.s==t2.s) return MM;   //同一类型的点间距离设置为MM
return (double)sqrt((t1.x-t2.x)*(t1.x-t2.x+0.0)+(t1.y-t2.y)*(t1.y-t2.y));   //这里别忘了+0.0,将结果在sqrt里转化为double型,否则会compile error
}
double closest(int left,int right){   //最近点对模板
if(left==right) return MM;
if(left+1==right) return dis(a[left],a[right]);
int mid=(left+right)/2;
double d=min(closest(left,mid),closest(mid+1,right));  //左半边和右半边最小距离
//中间部分最小距离
int t=0;
for(int i=left;i<=right;i++){           //离mid点横坐标差在d范围内的所有点
if(fabs(a[i].x-a[mid].x+0.0)<=d)
b[t++]=a[i];
}
sort(b,b+t,cmp2);        //按y坐标排序
for(int i=0;i<t;i++)
for(int j=i+1;j<t;j++){
if(a[j].y-a[i].y<=d)           //只要判断y坐标差在d范围内的点即可
d=min(d,dis(a[i],a[j]));
}
return d;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld %lld",&a[i].x,&a[i].y);
a[i].s=0;
}
for(int i=n+1;i<=2*n;i++){
scanf("%lld %lld",&a[i].x,&a[i].y);
a[i].s=1;
}
sort(a+1,a+2*n+1,cmp1);
printf("%.3lf\n",closest(1,2*n));  //注意输出格式
}
return 0;
}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: