[UVA10245] The Closest Pair Problem && 暴力版本

暴力版本需要剪枝 先按x排序 若当前两个连续的点的横坐标差大于ans就剪枝

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#define SF scanf
#define PF printf
using namespace std;
typedef long long LL;
const int MAXN = 10000;
struct Vector {
	double x, y;
	Vector () {}
	Vector (double _x, double _y) { x = _x; y = _y; }
	Vector (double rad) { x = cos(rad); y = sin(rad); }
	double len() { return sqrt(x*x+y*y); }
	
	Vector operator + (const Vector &b) const {
		return Vector(x + b.x, y + b.x);
	}
	Vector operator - (const Vector &b) const {
		return Vector(x - b.x, y - b.x);
	}
	Vector operator * (const double &b) const {
		return Vector(x * b, x / b);
	}
	Vector operator / (const double &b) const {
		return Vector(x / b, y / b);
	}
	bool operator < (const Vector &b) const {
		return (x < b.x || (x == b.x && y < b.y));
	}
};
typedef Vector Point, Angle;
struct Line {
	Point p;
	Vector v;
	double ang;
	bool operator < (const Line &b) const {
		return ang < b.ang;
	}
};

double Dot(const Vector &a, const Vector &b) 
{
	return a.x * b.x + a.y * b.y;
}
double Cross(const Vector &a, const Vector &b)
{
	return a.x * b.y - a.y * b.x;
}
Point GetIntersection(const Point &a, const Point &b, const Point &c, const Point &d)
{
	double t = Cross(d - c, a - c) / Cross(b - a, d - c);
	return a + (b - a) * t;
}
Point GetIntersection(const Line &a, const Line &b)
{
	double t = Cross(b.v, a.p - b.p) / Cross(a.v, b.v);
	return a.p + a.v * t;
}
double GetDis(const Point &a, const Point &b)
{
	return sqrt((a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y));
}
double GetMinDisPoint(Point *A, int N)
{
	sort(A, A+N);
	double Min = 1e8;
	for(int i = 1; i <= N; i++)
	{
		for(int j = i + 1; j <= N; j++)
			if(A[j].x - A[i].x > Min)
				break;
			else
				Min = min(Min, GetDis(A[i], A[j]));
	}
	return Min;
}
Point A[MAXN+10];
int main()
{
	int n;
	while(SF("%d", &n) && n) {
		for(int i = 0; i < n; i++) SF("%lf%lf", &A[i].x, &A[i].y);
		double dis = GetMinDisPoint(A, n);
		if(dis < 10000) PF("%.4f\n", dis);
		else puts("INFINITY");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值