POJ3714 Raid 平面最近点对 [喜闻乐见的暴力算法]

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

这里是POJ3714:『那么,开始我们的战争(Date)吧』的题解。

这里是原文就是我的wp博客啦上这里看效果更好哦点一下玩一年就看一下嘛求你啦!

题目大意:平面上有n对小哥哥和小姐姐,给出他们的坐标,距离最近的一对小哥哥小姐姐将会在一起,求他们的坐标。

--“Akaline,你又乱编题面了!”

--“读书人的事,能叫编吗?这叫修改!”

即求平面最近点对。

正确做法是分治。然而,对于Akarline这种人,当然是打暴力了(似乎还tm跑的比分治快还tm比分治短惊不惊喜意不意外)。

枚举每个点i然后枚举其他点j到这个点的距离就这么做

当然是会超时的了。

所以我们加个简单的剪枝。我们记最近点对距离为ans,以x排序后如果d[j].x-d[i].x>ans那么后面的点就不用枚举了直接break,原理显而易见。

这里我是不是可以使用营销手段点这里查看代码

算了还是不要了贴上吧。

#include 
#include 
#include 
#define MAXN 100050
using namespace std;
double ans;
int n, T;
struct Dot
{
	double x, y;
	bool flag;
} d[MAXN << 1];
double dis(Dot a, Dot b)
{
	return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
int cmp(Dot a, Dot b)
{
	return a.x < b.x;
}
int main()
{
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		for(int i = 1;i <= n;i++)
		{
			scanf("%lf%lf", &d[i].x, &d[i].y);
			d[i].flag = 0;
		}
		for(int i = n + 1;i <= n * 2;i++)
		{
			scanf("%lf%lf", &d[i].x, &d[i].y);
			d[i].flag = 1;
		}
		sort(d+1, d+n+n+1, cmp);
		ans = 2147483647.00;
		for(int i = 1;i <= 2*n;i++)
			for(int j = i;j <= 2*n;j++)
			{
				if(d[i].flag == d[j].flag) continue;
				ans = min(ans, dis(d[i], d[j]));
				if(d[j].x - d[i].x >= ans) break;
			}
		printf("%.3lf\n", ans);
	}
}

你可能感兴趣的:(OIer在成长)