UVA 11796 Dog Distance

来源:互联网 发布:其入之不亦深乎 编辑:程序博客网 时间:2024/05/13 20:57



注意最后结果是四舍五入到最近的整数

#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
double Min,Max;
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
};
Point operator+(Point A,Point B)
{
    return Point(A.x+B.x,A.y+B.y);
}
Point operator-(Point A,Point B)
{
    return Point(A.x-B.x,A.y-B.y);
}
Point operator*(Point A,double p)
{
    return Point(A.x*p,A.y*p);
}
Point operator/(Point A,double p)
{
    return Point(A.x/p,A.y/p);
}
int dcmp(double x)//相等函数
{
    double eps=1e-10;
    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 Cross(Point A,Point B)//求叉积
{
    return A.x*B.y-A.y*B.x;
}
double Length(Point A)
{
    return sqrt(Dot(A,A));
}
double DistanceToSegment(Point P,Point A,Point B)
{
    if(A==B)return Length(P-A);
    Point 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);
}
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;
    cin>>T;
    for(int cases=1;cases<=T;cases++)
    {
        int A,B;
        Point P[60],Q[60];
        cin>>A>>B;
        for(int i=0;i<A;i++)//输入点的坐标
            cin>>P[i].x>>P[i].y;
        for(int i=0;i<B;i++)
            cin>>Q[i].x>>Q[i].y;
        double lena=0,lenb=0;
        for(int i=0;i<A-1;i++)//求总的线段长度
            lena=lena+Length(P[i+1]-P[i]);
        for(int i=0;i<B-1;i++)
            lenb=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++;
        }
        cout<<"Case "<<cases<<": "<<int(Max-Min+0.5)<<endl;
    }
    return 0;
}




0 0