【UVa 11796】Dog Distance (计算几何)

Description

两条狗分别沿折线奔跑,同时出发、同时到达,且匀速奔跑,求奔跑过程中两条狗的最远距离、最近距离之差。

Solution

模拟整个过程,将时间分段使每段时间两条狗都是直线运动,将一条狗设为参照物,求另一条狗到该线段的距离即可。

开始求点到线段距离时没有判断线段退化成一个点的情况,导致调了几个小时。。。

Source

/**************************************
 * Au: Hany01
 * Date: [UVa 11796] Dog Distance
 * Prob: Feb 10th, 2018
 * Email: hany01@foxmail.com
**************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void File()
{
#ifdef hany01
    freopen("uva11796.in", "r", stdin);
    freopen("uva11796.out", "w", stdout);
#endif
}

const double eps = 1e-7;

struct Point
{
    double x, y;
    Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
int dcmp(double x) { if (fabs(x) < eps) return 0; return x < 0 ? -1 : 1; }
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 !dcmp(A.x - B.x) && !dcmp(A.y - B.y); }
double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double DistanceToLine(Point P, Point A, Point B) { Vector v1 = P - A, v2 = B - A; return fabs(Cross(v1, v2)) / Length(v2); }
double DistanceToSegment(Point P, Point A, Point B) {
    if (A == B) return Length(P - A);
    if (dcmp(Dot(P - A, B - A)) < 0) return Length(P - A);
    if (dcmp(Dot(P - B, A - B)) < 0) return Length(P - B);
    return DistanceToLine(P, A, B);
}

const int maxn = 55;

int A, B, Case;
double LenA, LenB, Max, Min;
Point P[maxn], Q[maxn];

inline void Init()
{
    A = read(), B = read(), LenA = LenB = 0.0;
    For(i, 1, A) scanf("%lf%lf", &P[i].x, &P[i].y);
    For(i, 1, B) scanf("%lf%lf", &Q[i].x, &Q[i].y);
    For(i, 1, A - 1) LenA += Length(P[i] - P[i + 1]);
    For(i, 1, B - 1) LenB += Length(Q[i] - Q[i + 1]);
}

inline void Update(Point P, Point A, Point B) {
    chkmin(Min, DistanceToSegment(P, A, B)), chkmax(Max, Length(P - A)), chkmax(Max, Length(P - B));
}

inline void Solve()
{
    Point Pa = P[1], Pb = Q[1];
    int cura = 1, curb = 1;
    Min = 1e9, Max = -1e9;
    while (cura < A && curb < B) {
        double resa = Length(P[cura + 1] - Pa), resb = Length(Q[curb + 1] - Pb);
        double t = min(resa / LenA, resb / LenB);
        Vector va = (P[cura + 1] - Pa) / resa * t * LenA, vb = (Q[curb + 1] - Pb) / resb * t * LenB;
        Update(Pa, Pb, Pb + vb - va);
        Pa = Pa + va, Pb = Pb + vb;
        while (Pa == P[cura + 1]) ++ cura;
        while (Pb == Q[curb + 1]) ++ curb;
    }
    printf("Case %d: %.0lf\n", ++ Case, Max - Min);
}

int main()
{
    File();
    for (register int T = read(); T --; ) {
        Init();
        Solve();
    }
    return 0;
}
//圆荷浮小叶,细麦落轻花。
//    -- 杜甫《为农》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值