HDU 3400 Line belt 三分

题意:就是给你两条线段AB , CD ,一个人在AB上跑速度p, 在CD上跑q,在其他地方跑速度是r

问你从A到D最少的时间是多少

#include<stdio.h>
#include<string.h>
#include<math.h>
#define eps 1e-6
#define INF 9999999999.0


int P, Q, R;

struct point
{
	double x, y;
}A, B, C, D;

point getMid(point a, point b)
{
	point c;
	c.x = (a.x + b.x) * 1.0 / 2;
	c.y = (a.y + b.y) * 1.0 / 2;
	return c;
}

double getDistance(double x1, double y1, double x2, double y2)
{
	return sqrt((x1-x2)*(x1-x2)*1.0 + (y1-y2)*(y1-y2)*1.0);
}

double getTime(point a, point b, int v)
{
	double dis = getDistance(a.x, a.y, b.x, b.y);
	double t = dis / (v * 1.0);
	return t;
}

double search1(point p)
{
	double t1 = 0, t2 = 1;
	point l = C, r = D, mid1, mid2;
	while(fabs(t1 - t2) > eps)
	{
		mid1 = getMid(l, r);
		mid2 = getMid(mid1, r);
		t1 = getTime(p, mid1, R) + getTime(D, mid1, Q);
		t2 = getTime(p, mid2, R) + getTime(D, mid2, Q);
		if(t1 - t2 > eps)
			l = mid1;
		else if( t1 - t2 < -eps)
			r = mid2;
		else return t1;	
	}
	return t1;
}

double search2()
{
	double t1 = 0, t2 = 1;
	point l, r, mid1, mid2;
	l = A; 
	r = B;
	while(fabs(t1 - t2) > eps)
	{
		mid1 = getMid(l, r);
		mid2 = getMid(mid1, r);
		t1 = getTime(A, mid1, P) + search1(mid1);
		t2 = getTime(A, mid2, P) + search1(mid2);
		if(t1 - t2 > eps)
			l = mid1;
		else if( t1 - t2 < -eps)
			r = mid2;
		else return t1;
	}
	return t1;
}



int main()
{
	int t;
	scanf("%d", &t);
	while( t-- )
	{
		scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y);
		scanf("%lf%lf%lf%lf", &C.x, &C.y, &D.x, &D.y);
		scanf("%d%d%d", &P, &Q, &R);
		printf("%.2lf\n", search2());
	}
	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值