| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 8248 | Accepted: 2460 |
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
Source
POJ Founder Monthly Contest 2008.12.28, Dagger题目:http://poj.org/problem id=3714
计算几何求最近点对的变种。 如何求最近点对,在 http://blog.csdn.net/lttree/article/details/25156173 中已经介绍过。 这个题属于变种吧,要求两个点分别属于不同集合。 其实也很简单,按照上面的代码,稍微改点地方就OK了。 就是在返回长度的时候判断是否属于同一集合,若属于同一个集合,就返回一个很大的数,我用的999999。
/************************************** *************************************** * Author:Tree * *From :http://blog.csdn.net/lttree * * Title : Raid * *Source: poj 3714 * * Hint : 计算几何――最近点对 * *************************************** **************************************/ #include#include #include using namespace std; // 不光有点,还要有集合类别标志 struct Point { double x,y; int flag; }p[1000001]; int arr[1000001]; double Min(double a,double b) { return a >1; double ans=Min(close_pair(l,mid),close_pair(mid+1,r)); int i,j,cnt=0; for(i=l; i<=r; ++i) if( p[i].x>=p[mid].x-ans && p[i].x<=p[mid].x+ans ) arr[cnt++]=i; sort(arr,arr+cnt,cmp_y); for( i=0; i =ans) break; ans=Min(ans,dis(p[arr[i]],p[arr[j]])); } } return ans; } int main() { int i,n,t; scanf("%d",&t); while( t-- ) { scanf("%d",&n); for(i=0;i