hdoj_3400Line belt

Line belt

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1885    Accepted Submission(s): 713


Problem Description
In a two-dimensional plane there are two line belts, there are two segments AB and CD, lxhgww's speed on AB is P and on CD is Q, he can move with the speed R on other area on the plane.
How long must he take to travel from A to D?
 

Input
The first line is the case number T.
For each case, there are three lines.
The first line, four integers, the coordinates of A and B: Ax Ay Bx By.
The second line , four integers, the coordinates of C and D:Cx Cy Dx Dy.
The third line, three integers, P Q R.
0<= Ax,Ay,Bx,By,Cx,Cy,Dx,Dy<=1000
1<=P,Q,R<=10
 

Output
The minimum time to travel from A to D, round to two decimals.
 

Sample Input
1 0 0 0 100 100 0 100 100 2 2 1
 

Sample Output
136.60
 
三分嵌套三分。
AB上确定一点,然后三分枚举CD上的点。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double EPS = 1e-10;

int p,q,r;

struct point {
	double x;
	double y;
};

double dis(point a,point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double findy(point c,point d,point y)
{
	point mid,midmid,left,right;
	double mid_t,midmid_t;
	left = c;
	right = d;
	do
	{
		mid.x = (left.x + right.x) / 2;
		mid.y = (left.y + right.y) / 2;
		midmid.x = (right.x + mid.x) / 2;
		midmid.y = (right.y + mid.y) / 2;
		mid_t = dis(d,mid) / q + dis(mid,y) / r;
		midmid_t = dis(d,midmid) / q+dis(midmid,y) / r;
		if(mid_t > midmid_t)
			left = mid;
		else right = midmid;
	}while(fabs(mid_t - midmid_t)>EPS);
	return mid_t;
}
double find(point a,point b,point c,point d)
{
	point mid,midmid,left,right;
	double mid_t,midmid_t;
	left = a;
	right = b;
	do
	{
		mid.x = (left.x + right.x) / 2;
		mid.y = (left.y + right.y) / 2;
		midmid.x = (right.x + mid.x) / 2;
		midmid.y = (right.y + mid.y) / 2;
		mid_t = dis(a,mid) / p + findy(c,d,mid);
		midmid_t = dis(a,midmid) / p + findy(c,d,midmid);
		if(mid_t > midmid_t)left = mid;
		else right = midmid;
	}while(fabs(mid_t - midmid_t)>EPS);
	return mid_t;
}

int main()
{
	freopen("in.txt","r",stdin);
	int t;
	point a,b,c,d;
	cin>>t;
	while(t--)
	{
		cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y;
		cin>>p>>q>>r;
		printf("%.2lf\n",find(a,b,c,d));
	}
	
	return 0;
}


posted @ 2012-11-05 15:35  N3verL4nd  阅读(111)  评论(0编辑  收藏  举报