UVA 10245 The Closest Pair Problem(暴力剪枝过了。)

Problem J
The Closest Pair Problem

Input: standard input
Output: standard output
Time Limit: 8 seconds
Memory Limit: 32 MB

 

Given a set of points in a two dimensional space, you will have to find the distance between the closest two points.

 

Input

 

The input file contains several sets of input. Each set of input starts with an integer N (0<=N<=10000), which denotes the number of points in this set. The next line contains the coordinates of N two-dimensional points. The first of the two numbers denotes the X-coordinate and the latter denotes the Y-coordinate. The input is terminated by a set whose N=0. This set should not be processed. The value of the coordinates will 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 decimal point) which denotes the distance between the closest two points. If there is no such two points in 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

题意: 给定n个点。求两点的距离最小的距离。 如果小于10000输出。否则输出INFINITY。

思路: 数据量有1W。。直接暴力复杂度为n^2。。 据说这题是用分治的方法过。。但是剪了个枝也过了。。。详见代码

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

int n;
double Min;
struct Point {
    double x, y;
} p[10005];

int cmp (Point a, Point b) {
    return a.x < b.x;
}
int main() {
    while (~scanf("%d", &n) && n) {
	Min = 10000;
	for (int i = 0; i < n; i ++)
	    scanf("%lf%lf", &p[i].x, &p[i].y);
	sort(p, p + n, cmp);
	for (int i = 0; i < n ; i ++)
	    for (int j = i + 1; j < n; j ++) {
		if (p[j].x - p[i].x >= Min) break;//剪枝。。
		double sb = sqrt((p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y));
		if (Min > sb)
		    Min = sb;
	    }
	if (Min == 10000) printf("INFINITY\n");
	else printf("%.4lf\n", Min);
    }
    return 0;
}


你可能感兴趣的:(UVA 10245 The Closest Pair Problem(暴力剪枝过了。))