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.4140.000
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
因为数组开小了,调了一个小时。。。
最小点距离。
详情请移步 http://blog.csdn.net/senyelicone/article/details/51817491
(顺便打广告)
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const double maxx=1e50; int n,t; struct node{ double x,y; bool kk; }a[200006],c[200006]; double min(double u,double v) { return u<v ? u:v; } inline bool kx(const node &u,const node &v) //直接写会超时 { return u.x<v.x; //并不是三目运算符 } double ab(double u) { return u>=0 ? u:-u; } inline bool ky(const node &u,const node &v) { return u.y<v.y; } double kl(const node &u,const node &v) //距离 { if(u.kk==v.kk) return maxx; return sqrt((u.x-v.x)*(u.x-v.x)+(u.y-v.y)*(u.y-v.y)); } inline double findd(int u,int v) //int型 { if(u==v) return maxx; int i,j,nod=0,l=(u+v)/2; double ans=min(findd(u,l),findd(l+1,v)); //一定要写成double型 for(i=u;i<=v;i++) if(ab(a[i].x-a[l].x)<=ans) c[++nod]=a[i]; /**/ sort(c+1,c+nod+1,ky); for(i=1;i<=nod;i++) for(j=i+1;j<=nod;j++) { if((c[j].y-c[i].y)>ans) break; ans=min(ans,kl(c[j],c[i])); } return ans; } int main() { int i; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%lf%lf",&a[i].x,&a[i].y); a[i].kk=0; } for(i=1;i<=n;i++) { scanf("%lf%lf",&a[i+n].x,&a[i+n].y); a[i+n].kk=1; } sort(a+1,a+2*n+1,kx); printf("%.3f\n",findd(1,2*n)); } }