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

来源:互联网 发布:格罗兹尼巷战 知乎 编辑:程序博客网 时间:2024/05/29 15:28

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

#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<set>#include<map>#include<iostream>#define SF scanf#define PF printfusing 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;elseMin = 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