UVA 11796 - Dog Distance

代码打错了好多次,结果好多次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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值