您的位置:首页 > 其它

uva 11796 俩狗问题(2维计算集合终极模板)

2015-04-06 21:39 323 查看
题意:

甲乙俩狗沿一条这线跑,速度未知,同时出发,同时到达,且匀速奔跑。

求甲乙在夕阳下奔跑的过程中最远距离和最近距离之差。

解析:

刘汝佳 Nbbbbbbbb.

先看最简单的情况,甲和乙都沿着一条线段跑。

由运动的相对性可以令甲静止,则乙向一个合成的方向跑,如图:



所以现在我只要求a点到点Pb与Pb + Vb - Va这条直线的最大最小距离就行啦。

换到好多点,只要判断谁先到点上就行了。

代码注释很清晰了,结合图像应该就可以理解啦。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long

using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

/////////////////////////////////////////////////////
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 - (Point A, Point 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;
}
else
{
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 Angle(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(Point A, Point B, Point 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));
}

Vector Normal(Vector A)//µ¥Î»·¨Ïß turn left 90 degrees
{
double L = Length(A);
return Vector(-A.y / L, A.x / L);
}

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;
}

double DistanceToLine(Point P, Point A, Point B)
{
Vector v1 = B - A;
Vector v2 = P - A;
return fabs(Cross(v1, v2)) / Length(v1);
}

double DistanceToSegment(Point P, Point A, Point B)
{
if (A == B)
{
return Length(P - A);
}
Vector v1 = B - A;
Vector v2 = P - A;
Vector 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);
}

bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
{
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
double 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 ConvexPolygonArea(Point* p, int n)
{
double area = 0;
for (int i = 1; i < n - 1; i++)
{
area += Cross(p[i] - p[0], p[i + 1] - p[0]);
}
return area / 2.0;
}

Point readPoint()
{
double x, y;
scanf("%lf %lf", &x, &y);
return Point(x, y);
}

//////////////////////////////////////////////////////

const int maxn = 60;
int T, A, B;
Point P[maxn], Q[maxn];
double Min, Max;

void update(Point P, Point A, Point B)
{
Min = min(Min, DistanceToSegment(P, A, B));//点到直线最小距离。
Max = max(Max, max(Length(P - A), Length(P - B)));//点到直线最大距离,只能是两边界点。
}

int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
int ncase;
scanf("%d", &ncase);
int ca = 1;
while (ncase--)
{
scanf("%d%d", &A, &B);
for (int i = 0; i < A; i++)
P[i] = readPoint();
for (int i = 0; i < B; i++)
Q[i] = readPoint();

///将A和B的长度做为其速度,因为时间恰好相等,为1.
double lenA = 0, lenB = 0;
for (int i = 0; i < A - 1; i++)
lenA += Length(P[i + 1] - P[i]);
for (int i = 0; i < B - 1; i++)
lenB += Length(Q[i + 1] - Q[i]);

int Sa = 0, Sb = 0;
Point Pa = P[0], Pb = Q[0];
Min = inf, Max = -inf;
while (Sa < A - 1 && Sb < B - 1)
{
double La = Length(P[Sa + 1] - Pa);
double Lb = Length(Q[Sb + 1] - Pb);
//取一个时间,看谁先到这个点。
double T = min(La / lenA, Lb / lenB);
///位移向量 =    单位方向向量      * t * v
Vector Va = ((P[Sa + 1] - Pa) / La) * T * lenA;
Vector Vb = ((Q[Sb + 1] - Pb) / Lb) * T * lenB;
///Pa 看做定点,运动合成,Pb向Vb - Va方向移动
update(Pa, Pb, Pb + Vb - Va);
Pa = Pa + Va;
Pb = Pb + Vb;
if (Pa == P[Sa + 1])
Sa++;
if (Pb == Q[Sb + 1])
Sb++;
}
printf("Case %d: %.0lf\n", ca++, Max - Min);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: