您的位置:首页 > 大数据 > 人工智能

POJ 3714 Raid

2016-05-15 21:48 309 查看
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


在模版里的dis函数中特别地把相同集合的点距设为INF

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 200060;

// 分治算法求最近点对
struct point//保存每一个点
{
double x,y;
int mm;
} p[maxn];

int a[maxn];//保存筛选的坐标点的索引即2min(dl,dh)区间的坐标点索引

int cmpx(point a,point b)//对n个点按横坐标由小到大排序
{
return a.x<b.x;
}

int cmpy(int a,int b)//(这里用的是下标索引)对筛选的点按纵坐标由小到大排序
{
return p[a].y<p[b].y;
}

inline double dis(point a,point b)//求点对间的距离
{
if(a.mm==b.mm) return INF;
else
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double closest(int low,int high)//求最近点对
{
int i,j,k;
if(low+1==high)//只有两个点
return dis(p[low],p[high]);
if(low+2==high)//只有三个点
return min(dis(p[low],p[high]),min(dis(p[low],p[low+1]),dis(p[low+1],p[high])));
int mid=(low+high)>>1;//求中点即左右子集的分界线
double d=min(closest(low,mid),closest(mid+1,high));
for(i=low,k=0; i<=high; i++) //把x坐标在p[mid].x-d ~ p[mid].x+d范围内的点筛选出来
{
if(p[i].x >=p[mid].x-d&&p[i].x<=p[mid].x+d)
a[k++]=i;//保存这些点的下标索引
}
sort(a,a+k,cmpy);//按y坐标进行升序排序
for(i=0; i<k; i++)
{
for(j=i+1; j<k; j++)
{
if(p[a[j]].y-p[a[i]].y>=d)//注意下标索引
break;
d=min(d,dis(p[a[i]],p[a[j]]));
}
}
return d;
}
int main()
{
int i,n,T;
double MIN,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);//n个点
MIN=INF;
for(i = 0 ; i < n ; ++i)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
p[i].mm=0;
}
for(i = n ; i < n*2 ; ++i)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
p[i].mm=1;
}
sort(p , p + n*2 , cmpx);
printf("%.3f\n",closest(0 , n*2-1 ));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: