最近点对问题:模板题
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define eps 1e-6
const int maxn = 10000+10;
struct node{
double x,y;
};
int n,ss[maxn];
double ans;
node aa[maxn];
int cmp(double x){
if (fabs(x)<eps){
return 0;
}
if(x > 0){
return 1;
}
return -1;
}
double dist(node a,node b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool compx(int i, int j){
return cmp(aa[i].x-aa[j].x)<0;
}
bool compy(int i,int j){
return cmp(aa[i].y-aa[i].y) <0;
}
double min_dist(int l,int r){
double ans = 1e100;
if(r-l<20){
for(int i = l; i < r; i++){
for(int j = i+1; j < r; j++){
double tmp = dist(aa[ss[i]],aa[ss[j]]);
//cout<<tmp<<endl;
ans = min(tmp,ans);
}
}
return ans;
}
int tl,tr,m = (l+r)/2;
ans = min(min_dist(l,m),min_dist(m,r));
for(tl = l; aa[ss[tl]].x < aa[ss[m]].x-ans; tl++);
for(tr = r-1; aa[ss[tr]].x > aa[ss[m]].x+ans;tr--);
sort(ss+tl,ss+tr,compy);
for(int i = tl; i < tr; i++){
for(int j = i+1; j < min(tr,i+6); j++){
double tmp = dist(aa[ss[i]],aa[ss[j]]);
ans = min(ans,tmp);
}
}
sort(ss+tl,ss+tr,compx);
return ans;
}
double Min_dist(int n){
for(int i =0; i < n; i++) ss[i] = i;
sort(ss,ss+n,compx);
return min_dist(0,n);
}
int main(){
while(~scanf("%d",&n) && n){
for(int i = 0; i < n; i++){
scanf("%lf%lf",&aa[i].x,&aa[i].y);
}
double ans = Min_dist(n);
if(ans > 10000.0){
cout<<"INFINITY"<<endl;
}else{
printf("%.4lf\n",ans);
}
}
return 0;
}