uva 11796 - Dog Distance

#include<iostream>

#include<cstdio>

#include<cmath>

#define eps 1e-8

#define max(a,b) a>b?a:b



using namespace std;



int sig(double a)

{

    return (a>eps)-(a<-eps);

}



typedef struct point

{

    double x,y;

    point(double xx=0,double yy=0):x(xx),y(yy){}

}vector;



vector operator - (point a,point b)

{

    return vector(a.x-b.x,a.y-b.y);

}

point operator + (point a,vector b)

{

    return point(a.x+b.x,a.y+b.y);

}

vector operator * (vector a,double b)

{

    return vector(a.x*b,a.y*b);

}

double dot(vector a,vector b)

{

    return a.x*b.x+a.y*b.y;

}

double len(vector a)

{

    return sqrt(dot(a,a));

}

double cross(vector a,vector b)

{

    return a.x*b.y-a.y*b.x;

}

vector resiz(vector a,double l)

{

    l/=len(a);

    return vector(a.x*l,a.y*l);

}

double dis(point p,point a,vector v)

{

    point b=a+v;

    vector v1=p-a,v2=p-b;

    if(sig(dot(v,v1))<=0) return len(v1);

    else if(sig(dot(v,v2))>=0) return len(v2);

    return fabs(cross(v1,v))/len(v);

}



int main()

{

    int i,j,m,n,t,c=0;

    double mx,mn,v1,v2,l1,l2,d;

    point p[51],q[51],a,b;

    scanf("%d",&t);

    while(t--)

    {

        scanf("%d%d",&m,&n);

        v1=v2=0;

        for(i=0;i<m;i++)

        {

            scanf("%lf%lf",&p[i].x,&p[i].y);

            if(i) v1+=len(p[i]-p[i-1]);

        }

        for(i=0;i<n;i++)

        {

            scanf("%lf%lf",&q[i].x,&q[i].y);

            if(i) v2+=len(q[i]-q[i-1]);

        }

        mn=mx=len(p[0]-q[0]);

        a=p[0];b=q[0];

        for(i=1,j=1;i<m;)

        {

            l1=len(p[i]-a);l2=len(q[j]-b);

            d=l1/v1-l2/v2;

            if(sig(d)>0)

            {

                vector w=resiz(p[i]-a,l2*v1/v2),u=w-(q[j]-b);

                d=dis(b,a,u);

                if(sig(mn-d)>0) mn=d;

                d=max(len(b-a),len(b-a-u));

                if(sig(mx-d)<0) mx=d;

                a=a+w;b=q[j++];

            }

            else if(sig(d)<0)

            {

                vector w=resiz(q[j]-b,l1*v2/v1),u=w-(p[i]-a);

                d=dis(a,b,u);

                if(sig(mn-d)>0) mn=d;

                d=max(len(b-a),len(a-b-u));

                if(sig(mx-d)<0) mx=d;

                b=b+w;a=p[i++];

            }

            else

            {

                vector u=(q[j]-b)-(p[i]-a);

                d=dis(a,b,u);

                if(sig(mn-d)>0) mn=d;

                d=max(len(b-a),len(a-b-u));

                if(sig(mx-d)<0) mx=d;

                a=p[i++];b=q[j++];

            }

        }

        printf("Case %d: %.lf\n",++c,mx-mn);

    }

    return 0;

}


你可能感兴趣的:(uva)