题目大意:
甲和乙两条狗分别沿着一条折线跑,它们速度未知,但同时出发并且同时到达终点,并且都是匀速奔跑。求奔跑过程中两只狗的最大距离与最小距离之差。
思路:
因为运动是相对的,我们可以认为甲静止不动,乙沿着直线走。则问题就转化为点到线段的最小距离。
那么,我们对于每段分析,看谁先到达该线段的终点,则该段可以用上面的方法求。
把a看成不动的,则有b运动到了cb+vb-va(其中va,vb分别为a和b的位移向量,ca,cb分别为a和b初始位置)
那么就转换为sa到线段cb+vb-va的距离。(最小距离为点到直线,而最大距离一定在线段两边)
至于速度的表示,设1S到达终点,那么速度就是总长。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <set>
#include <list>
#include <queue>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
const int N = 100005;
const double eps = 1e-10;
struct Point
{
double x,y;
Point(double x = 0,double y = 0):x(x),y(y){}
Point operator +(Point a)
{
return Point(x + a.x,y + a.y);
}
Point operator -(Point a)
{
return Point(x - a.x,y-a.y);
}
Point operator *(double p)
{
return Point(x * p,y * p);
}
Point operator /(double p)
{
return Point(x / p,y / p);
}
}P[66],Q[66];
int A,B;
double Min,Max;
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(Point A,Point B)
{
return A.x * B.x + A.y * B.y;
}
double Length(Point A)
{
return sqrt(Dot(A,A));
}
double Angle(Point A,Point B)
{
return acos(Dot(A,B) / Length(A) / Length(B));
}
double Cross(Point A,Point B)
{
return A.x*B.y - A.y*B.x;
}
Point Rotate(Point A,double rad)
{
return Point(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
Point GetLineIntersection(Point P,Point v,Point Q,Point w)
{
Point u = P - Q;
double t = Cross(w,u) / Cross(v,w);
return P + v*t;
}
Point get(Point A,Point B,Point C)
{
Point v1 = C-B;
double a1 = Angle(A-B,v1);
//printf("%.6lf\n",a1);
v1 = Rotate(v1,a1/3);
//printf("%.6lf %.6lf\n",v1.x,v1.y);
Point v2 = B-C;
double a2 = Angle(A - C,v2);
//printf("%.6lf\n",a2);
v2 = Rotate(v2,-a2/3);
//printf("%.6lf %.6lf\n",v2.x,v2.y);
return GetLineIntersection(B,v1,C,v2);
}
double DistanceToSegment(Point P,Point A,Point B)
{
if(A == B)
return Length(P-A);
Point v1 = B - A;
Point v2 = P - A;
Point 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);
}
void update(Point P,Point A,Point B)
{
Min = min(Min,DistanceToSegment(P,A,B));
Max = max(Max,Length(P-A));
Max = max(Max,Length(P-B));
}
int main()
{
int t,n,c = 1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&A,&B);
for(int i = 0; i < A; i++)
scanf("%lf%lf",&P[i].x,&P[i].y);
for(int i = 0; i < B; i++)
scanf("%lf%lf",&Q[i].x,&Q[i].y);
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 = 1e9,Max = -1e9;
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);
Point Va = (P[Sa+1] - Pa) / La * T * LenA;
Point Vb = (Q[Sb+1] - Pb) / Lb * T * LenB;
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",c++,Max-Min);
}
return 0;
}