UVa 10245 The Closest Pair Problem (计算几何&最近点对)

262 篇文章 0 订阅
40 篇文章 0 订阅

10245 - The Closest Pair Problem

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1186

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


排序然后挨个求距离。

O(Nlog N)的复杂度。


完整代码:

/*0.066s*/

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

struct Point
{
	double x, y;
	bool operator < (const Point& a) const
	{
		return x < a.x;
	}
} point[10010];

int main()
{
	int n, i, j;
	while (scanf("%d", &n), n)
	{
		double minD = 10000.0;
		for (i = 0; i < n; ++i)
			scanf("%lf%lf", &point[i].x, &point[i].y);
		sort(point, point + n);
		for (i = 0; i < n - 1; ++i)
			for (j = i + 1; j < n && point[j].x - point[i].x < minD; ++j)
				minD = min(minD, hypot(point[j].x - point[i].x, point[j].y - point[i].y));
		if (minD >= 10000) puts("INFINITY");
		else printf("%.4f\n", minD);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值