Link
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1186
"Original question"
Given a set of points in a two dimensional spaces, you'll have to find the distance between the closest two.
Input
The input file contains several sets of input. Each set of input starts with a integer N (0<=n<=10000), which denotes the number of points in this set. The next n line contains the coordinates of n two-dimensional points. The the two numbers denotes the x-coordinate and the latter denotes. The input is terminated by a set whose n=0. This set should is processed. The value of the coordinates would be less than 40000 and non-negative.
Output
For each set of input produce a single line of output containing a floating point number (with four digits after the DECIM Al point) which denotes the distance between the closest two. If There is no such two points into the input whose distance is less than-10000, print the line INFINITY.
Sample Input
3
0 0
10000 10000
20000 20000
5
0 2
6 67
43 71
39 107
189 140
0
Sample Output
INFINITY
36.2215
"The main effect of the topic"
The coordinates of the points on the N-plane are given and the shortest distance between the points is required. If the shortest distance exceeds 10000 save the output infinity
"Analysis and Summary"
In computational geometry, "find the nearest point to" the problem, "Introduction to the Algorithm," chapter 33.4 is devoted to this algorithm, with the idea of partition, the complexity of O (n lg N). But the realization is still very troublesome.
This column more highlights: http://www.bianceng.cn/Programming/sjjg/
The method here is O (n lg n LG N), which basically simplifies one of the steps, the following sort, so that the complexity of this step is changed from O (n) to O (n lg N).
Code
* * * uva:10245-the closest Pair Problem * time:0.080 * author:d_double * */#include <iostream&
Gt #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace
Std
Const double INF = 1E20;
const int N = 100005;
struct point{double x;
Double y; friend BOOL operator < (const point& A, const point& b) {if (a.x!= b.x) return a.x <
b.x;
return A.Y < B.y;
}}point[n];
int n;
int tmpt[n];
BOOL Cmpy (const int& A, const int& b) {return point[a].y < point[b].y;
Double min (double A, double b) {return a < b a:b; double dis (int i, int j) {return sqrt (point[i].x-point[j].x) * (point[i].x-point[j].x) +
(POINT[I].Y-POINT[J].Y) * (POINT[I].Y-POINT[J].Y)); Double Closest_pair (int left, int right) {
Double d = INF;
if (left==right) return D;
if (left + 1 = right) return to dis (left, right);
int mid = (left+right) >>1;
Double D1 = Closest_pair (left,mid);
Double D2 = Closest_pair (mid+1,right);
d = min (d1,d2);
int i,j,k=0; Separates the range for the width of D to (i = left; I <= right; i++) {if (Fabs (point[mid].x-point[i].x) <= D) TM
pt[k++] = i;
Sort (tmpt,tmpt+k,cmpy); Linear scan for (i = 0; i < K; i++) {for (j = i+1; J < k && J<=k+7 && point[tmpt[j]].y-p oint[tmpt[i]].y<d;
J + +) {double d3 = dis (tmpt[i],tmpt[j]);
if (d > d3) d = D3;
} return D;
int main () {freopen ("Input.txt", "R", stdin);
while (scanf ("%d", &n)!=eof) {if (n==0) break; for (int i = 0; i < n; i++) scanf ("%lf%lf", &point[i].x,&pOINT[I].Y);
Sort (point,point+n);
Double Ans=closest_pair (0,n-1);
if (ans<10000) printf ("%.4lf\n", ans);
else printf ("infinity\n");
return 0; }