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

POJ 3714 Raid(计算几何の最近点对)

2013-07-17 23:27 495 查看
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.

题目大意:给一个点集A,一个点集B,求min(distance(x, y))(x∈A,y∈B)

思路1:分治法。把点集按x坐标从小到大排序,mid = (left + right)/2,递归分治计算出左边部分和右边部分的最小距离mind,那么,若左半部分和右半部分存在一对点距离小于mind,那么这两个点一定在范围(x[mid] -mind ,x[mid] -mind)之间(因为在这之外的点与对面的点的距离必然大于mind)

思路2:暴力枚举+剪枝。暴力枚举每两个点之间的距离,若y[j] - y[i] >= mind则break(这个不用解释了吧……)

PS:要保留3位小数,题目没说,可以看样例

分治算法:1141MS

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <iostream>
#include <cmath>

using namespace std;

//维数
int K = 2;
long long ans;

struct kdNode
{
long long x[2];
int div;
};

struct qNode
{
long long r;
kdNode p;
qNode( long long _r, kdNode _p )
{
r = _r;
p = _p;
}
bool operator<( const qNode& x ) const
{
return r < x.r;
}
};

int cmpNo;
int cmp( kdNode a, kdNode b )
{
return a.x[cmpNo] < b.x[cmpNo];
}

long long dis2( kdNode& a, kdNode& b )
{
long long res = 0;
for( int i = 0; i < K; ++i )
res += (a.x[i]-b.x[i])*(a.x[i]-b.x[i]);
return res;
}

void buildKD( int s, int e, kdNode* p, int d )
{
if( s > e )    return;
int mid = (s+e)/2;
cmpNo = d;
nth_element(p+s, p+mid, p+e+1, cmp);
p[mid].div = d;
buildKD(s, mid-1, p, (d+1)%K);
buildKD(mid+1, e, p, (d+1)%K);
}

priority_queue<qNode> Q;

void findKD( int s, int e, kdNode tar, kdNode* p, int cnt )
{
if( s > e )    return;
int mid = (s+e)/2;
long long r = dis2(p[mid], tar);
ans = min(ans, r);
//t也许需要改成long long
long long t = tar.x[ p[mid].div ] - p[mid].x[ p[mid].div ];
if( t <= 0 )
{
findKD(s, mid-1, tar, p, cnt);
if( ans > t*t )
findKD(mid+1, e, tar, p, cnt);
}
else if( t > 0 )
{
findKD(mid+1, e, tar, p, cnt);
if( ans > t*t )
findKD(s, mid-1, tar, p, cnt);
}
}

kdNode p[50010], q;

int main()
{
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
int T, n, i, j;
int x, y;
scanf("%d", &T);
while( T-- )
{
scanf("%d", &n);
for( i = 0; i < n; ++i )
{
scanf("%d %d", &x, &y);
p[i].x[0] = x;
p[i].x[1] = y;
}
ans = 4100000000000000000LL;
buildKD(0, n-1, p, K-1);
for( i = 0; i < n; ++i )
{
scanf("%d %d", &x, &y);
q.x[0] = x;
q.x[1] = y;
findKD(0, n-1, q, p, 1);
}
printf("%.3f\n", sqrt(ans+0.));
}
return 0;
}


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