uva 11796 Dog Distance(向量)

https://vjudge.net/problem/UVA-11796

 

题意:甲和乙两条狗分别沿着不同的折线奔跑,速度未知,但是他们同时出发,同时到达,并且匀速奔跑

求甲和乙在奔跑过程中的最远距离与最近距离之差

 

因为运动是相对的,所以将甲看做不动

将运动分解为方向向量sa、sb和位移向量va、vb

那么乙相对于甲的运动为sb+vb-va

 

#include<cmath>
#include<cstdio>
#include<algorithm>

using namespace std;

struct Point 
{
    double x,y;
    Point(double x=0,double y=0) : x(x),y(y) { }
    void input() { scanf("%lf%lf",&x,&y); }
};

typedef Point Vector;

const double eps=1e-10;

int dcmp(double p) 
{
    if(fabs(p)<eps) return 0; return p<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 == (Vector A,Vector B) { return !dcmp(A.x-B.x) && !dcmp(A.y-B.y);  }

struct Geometry
{
    double Cross(Vector A,Vector B)
    {
        return A.x*B.y-A.y*B.x;
    }
    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 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))<0) return length(v2);
        if(dcmp(Dot(v1,v3))>0) return length(v3);
        return fabs(Cross(v1,v2)/length(v1));
    }    
};

Geometry Two_dimensional;

Point pa[51],pb[51];

double minn,maxn;

void update(Point P,Point A,Point B)
{
    minn=min(minn,Two_dimensional.DistanceToSegment(P,A,B));
    maxn=max(maxn,Two_dimensional.length(P-A));
    maxn=max(maxn,Two_dimensional.length(P-B));
}

int main()
{
    int T,suma,sumb,nowa,nowb;
    double lena,lenb;
    Point sa,sb;
    scanf("%d",&T);
    for(int t=1;t<=T;t++)
    {
        lena=lenb=0;
        minn=2e9; maxn=0;
        scanf("%d%d",&suma,&sumb);
        for(int i=1;i<=suma;i++) pa[i].input();
        for(int i=1;i<=sumb;i++) pb[i].input();
        for(int i=1;i<suma;i++) lena+=Two_dimensional.length(pa[i+1]-pa[i]);
        for(int i=1;i<sumb;i++) lenb+=Two_dimensional.length(pb[i+1]-pb[i]);
        nowa=nowb=1; sa=pa[1]; sb=pb[1];
        while(nowa<suma && nowb<sumb)
        {
            double la=Two_dimensional.length(pa[nowa+1]-sa);
            double lb=Two_dimensional.length(pb[nowb+1]-sb);
            double dis=min(la/lena,lb/lenb);
            Vector va=(pa[nowa+1]-sa)/la*dis*lena;
            Vector vb=(pb[nowb+1]-sb)/lb*dis*lenb;
            update(sa,sb,sb+vb-va);
            sa=sa+va;
            sb=sb+vb;
            if(sa==pa[nowa+1]) nowa++;
            if(sb==pb[nowb+1]) nowb++;
        }
        printf("Case %d: %.0lf\n",t,maxn-minn);
    }
}

 

posted @ 2017-06-10 11:14  TRTTG  阅读(292)  评论(0编辑  收藏  举报