代码打错了好多次,结果好多次WA。
代码如下:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
struct point{
double x,y;
point(double x=0,double y=0):x(x),y(y){}
};
typedef point vector;
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 b){return vector(a.x*b,a.y*b);}
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);}
const double eps=1e-10;
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(vector a,vector b){
return a.x*b.x+a.y*b.y;
}
double lenth(vector a){return sqrt(dot(a,a));}
double angle(vector a,vector b){return acos(dot(a,a))/lenth(a)/lenth(b);}
double cross(vector a,vector b){return a.x*b.y-a.y*b.x;}
double distancetoline(point p,point a,point b){
vector v1=b-a,v2=p-a;
return fabs(cross(v1,v2))/lenth(v1);
}
double distancetosegment(point p,point a,point b){
if(a==b)return lenth(p-a);
vector v1=b-a,v2=p-a,v3=p-b;
if(dcmp(dot(v1,v2))<0)return lenth(v2);
else if(dcmp(dot(v1,v3)>0))return lenth(v3);
else return fabs(cross(v1,v2))/lenth(v1);
}
point P[55],Q[55];
int main(){
// freopen("data.txt","r",stdin);
ios::sync_with_stdio(false);
int I;
cin>>I;
int kase=0;
while(I--){
int A,B;
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+=lenth(P[i+1]-P[i]);
for(int i=0;i<B-1;++i)lenb+=lenth(Q[i+1]-Q[i]);
int sa=0,sb=0;
point ta=P[0],tb=Q[0];
double maxn=1e-9,minn=1e9;
while(sa!=A-1&&sb!=B-1){
// cout<<sa<<' '<<sb<<endl;
double la=lenth(P[sa+1]-ta);
double lb=lenth(Q[sb+1]-tb);
double t=min(la/lena,lb/lenb);
vector va=(P[sa+1]-ta)/la*t*lena;
vector vb=(Q[sb+1]-tb)/lb*t*lenb;
minn=min(minn,distancetosegment(ta,tb,tb+(vb-va)));
maxn=max(maxn,lenth(ta-tb));
maxn=max(maxn,lenth(ta-tb-(vb-va)));
ta=va+ta;
tb=vb+tb;
if(ta==P[sa+1])sa++;
if(tb==Q[sb+1])sb++;
// cout<<maxn<<' '<<minn<<endl;
}
printf("Case %d: %.0f\n",++kase,maxn-minn);
}
return 0;
}