题意:  有两个狗, 按照 多边形跑,不知道两条狗的速度,但是狗是同时出发,同时到达终点的

输出两条狗的 最大相距距离 - 最小相距距离;

思路 : 用物理的相对运动来计算, 每次只计算 两条狗的直线运动, 转折点再额外更新

LRJ 模板大法好 !!!LRJ 模板大法好 !!!!LRJ 模板大法好 !!!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
const double eps = 1e-; struct Point {
double x , y;
Point (double x = , double y = ) : x(x),y(y) {}
}; typedef Point Vector; int dcmp (double x) { if(fabs(x) < eps) return ; else return x < ? - : ; } 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);
}
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 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 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)) < ) return Length(v2);
else if(dcmp(Dot(v1,v3)) > ) return Length(v3);
else return fabs(Cross(v1,v2)) / Length(v1);
} double MAX,MIN; void UpDate(Point P, Point A, Point B)
{
MIN = min(MIN, DistanceToSegment(P,A,B)); ///当前 和 点 到线段的最小距离
MAX = max(Length(P-A),MAX);
MAX = max(Length(P-B),MAX);
} Point Pa[maxn], Pb[maxn]; int main()
{
int t;
scanf("%d",&t);
for(int kase = ; kase <= t; kase++)
{
int A, B;
scanf("%d %d",&A,&B);
for(int i = ; i < A; ++i) cin >> Pa[i].x >> Pa[i].y;
for(int i = ; i < B; ++i) cin >> Pb[i].x >> Pb[i].y; double LenA = , LenB = ;
for(int i = ; i < A-; ++i) LenA += Length(Pa[i] - Pa[i+]);
for(int i = ; i < B-; ++i) LenB += Length(Pb[i] - Pb[i+]); MAX = -1e9; MIN = 1e9;
int sa = , sb = ; ///下一个转折点
Point Na = Pa[], Nb = Pb[]; /// 起始位置
while(sa < A- && sb < B-)
{
double La = Length(Pa[sa+] - Na); /// a 当前到下一个转折点的长度
double Lb = Length(Pb[sb+] - Nb); /// b ...
double t = min(La / LenA, Lb / LenB); /// 运动的时间
Vector Va = (Pa[sa+] - Na) / La * t * LenA;/// a 的位移量
Vector Vb = (Pb[sb+] - Nb) / Lb * t * LenB;/// b 的位移量
UpDate(Na,Nb,Nb+Vb-Va); /// B相对A 的运动就是 NB + Vb - Va;
Na = Na + Va; /// 更新 a 的当前点
Nb = Nb + Vb;
if(Na == Pa[sa+]) sa++; ///如果到了转折点, sa 下一个转折点更新
if(Nb == Pb[sb+]) sb++;
}
printf("Case %d: %.0lf\n",kase, MAX - MIN);
}
return ;
}
05-11 11:33