您的位置:首页 > 大数据 > 人工智能

UVA 11168 Airport(凸包)

2013-09-15 22:41 405 查看


UVA 11168 Airport(凸包)

分类: 计算几何2013-08-27
09:14 245人阅读 评论(1) 收藏 举报

给出平面上的n个点,求一条直线,使得所有点在该直线的同一侧且所有点到该直线的距离和最小,输出该距离和。

要使所有点在该直线的同一侧,明显是直接利用凸包的边更优。所以枚举凸包的没条边,然后求距离和。直线一般式为Ax + By + C = 0.点(x0, y0)到直线的距离为

fabs(Ax0+By0+C)/sqrt(A*A+B*B).由于所有点在直线的同一侧,那么对于所有点,他们的(Ax0+By0+C)符号相同,显然可以累加出sumX和sumY,然后统一求和。

[cpp] view
plaincopy

#include<algorithm>

#include<iostream>

#include<cstring>

#include<cstdlib>

#include<fstream>

#include<sstream>

#include<bitset>

#include<vector>

#include<string>

#include<cstdio>

#include<cmath>

#include<stack>

#include<queue>

#include<stack>

#include<map>

#include<set>

#define FF(i, a, b) for(int i=a; i<b; i++)

#define FD(i, a, b) for(int i=a; i>=b; i--)

#define REP(i, n) for(int i=0; i<n; i++)

#define CLR(a, b) memset(a, b, sizeof(a))

#define debug puts("**debug**")

#define LL long long

#define PB push_back

#define eps 1e-10

using namespace std;

struct Point

{

double x, y;

Point (double x=0, double y=0):x(x), y(y) {}

};

typedef Point Vector;

Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }

Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }

Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }

Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }

bool operator < (const Point& a, const Point& b)

{

return a.x < b.x || (a.x == b.x && a.y < b.y);

}

int dcmp(double x)

{

if(fabs(x) < eps) return 0;

return x < 0 ? -1 : 1;

}

bool operator == (const Point& a, const Point& b)

{

return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;

}

double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }

double Length(Vector A) { return sqrt(Dot(A, A)); }

double Angel(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }

double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }

double Area2(Vector A, Vector B, Vector C) { return Cross(B-A, C-A); }

//向量逆时针旋转

Vector Rotate(Vector A, double rad)

{

return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));

}

//求直线p+tv和q+tw的交点 Cross(v, w) == 0无交点

Point GetLineIntersection(Point p, Vector v, Point q, Vector w)

{

Vector u = p-q;

double t = Cross(w, u) / Cross(v, w);

return p + v*t;

}

//点p到直线ab的距离

double DistanceToLine(Point p, Point a, Point b)

{

Vector v1 = b - a, v2 = p - a;

return fabs(Cross(v1, v2)) / Length(v1);//如果不带fabs 得到的是有向距离

}

//点p到线段ab的距离

double DistanceToSegment(Point p, Point a, Point b)

{

if(a == b) return Length(p-a);

Vector v1 = b-a, v2 = p-a, v3 = p-b;

if(dcmp(Dot(v1, v2) < 0)) return Length(v2);

else if(dcmp(Dot(v1, v3)) > 0) return Length(v3);

else return fabs(Cross(v1, v2)) / Length(v1);

}

//点p在直线ab上的投影

Point GetLineProjection(Point p, Point a, Point b)

{

Vector v = b-a;

return a + v*(Dot(v, p-a) / Dot(v, v));

}

//点段相交判定

bool SegmentItersection(Point a1, Point a2, Point b1, Point b2)

{

double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1),

c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);

return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;

}

//点在线段上

bool OnSegment(Point p, Point a1, Point a2)

{

return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;

}

//多变形面积

double PolygonArea(Point* p, int n)

{

double ret = 0;

FF(i, 1, n-1) ret += Cross(p[i]-p[0], p[i+1]-p[0]);

return ret/2;

}

Point read_point()

{

Point a;

scanf("%lf%lf", &a.x, &a.y);

return a;

}

double torad(double d)//角度转弧度

{

return d/180 *acos(-1);

}

int ConvexHull(Point *p, int n, Point* ch)//凸包

{

sort(p, p+n);

int m = 0;

REP(i, n)

{

while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;

ch[m++] = p[i];

}

int k = m;

FD(i, n-2, 0)

{

while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;

ch[m++] = p[i];

}

if(n > 1) m--;

return m;

}

void getLineABC(Point A, Point B, double& a, double& b, double& c)//直线两点式转一般式

{

a = A.y-B.y, b = B.x-A.x, c = A.x*B.y-A.y*B.x;

}

const int maxn = 10001;

int n, T;

Point p[maxn], ch[maxn];

int main()

{

scanf("%d", &T);

FF(kase, 1, T+1)

{

scanf("%d", &n);

double X = 0, Y = 0, ans = 1e10;

REP(i, n) p[i] = read_point(), X += p[i].x, Y += p[i].y;

int m = ConvexHull(p, n, ch);

ch[m] = ch[0];

REP(i, m)

{

double a, b, c;

getLineABC(ch[i], ch[i+1], a, b, c);

ans = min(ans, fabs(a*X+b*Y+c*n)/(sqrt(a*a+b*b)));

}

printf("Case #%d: %.3f\n", kase, n > 2 ? ans/n : 0);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: