【BZOJ1857】[Scoi2010]传送带 三分套三分

【BZOJ1857】[Scoi2010]传送带

Description

在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段。两条传送带分别为线段AB和线段CD。lxhgww在AB上的移动速度为P,在CD上的移动速度为Q,在平面上的移动速度R。现在lxhgww想从A点走到D点,他想知道最少需要走多长时间

Input

输入数据第一行是4个整数,表示A和B的坐标,分别为Ax,Ay,Bx,By 第二行是4个整数,表示C和D的坐标,分别为Cx,Cy,Dx,Dy 第三行是3个整数,分别是P,Q,R

Output

输出数据为一行,表示lxhgww从A点走到D点的最短时间,保留到小数点后2位

Sample Input

0 0 0 100
100 0 100 100
2 2 1

Sample Output

136.60

HINT

对于100%的数据,1<= Ax,Ay,Bx,By,Cx,Cy,Dx,Dy<=1000
1<=P,Q,R<=10

题解:最终路径一定是先在AB上走x米,然后直线走到CD上,再走y米。那么如果x确定了,最终答案显然是一个关于y的单峰函数,可以直接三分(其实感觉可以O(1)算)。但是x的位置如何确定呢?x的位置也是一个单峰函数!证明不太会,网上有~

所以三分套三分即可。

 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
struct point
{
	double x,y;
	point() {}
	point(double a,double b) {x=a,y=b;}
	point operator + (const point &a) const {return point(x+a.x,y+a.y);}
	point operator * (const double &a) const {return point(x*a,y*a);}
}s1,t1,s2,t2;
double P,Q,R,ans;
inline double dis(point a,point b)
{
	return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}
inline double calc(point a,point b)
{
	double tmp=dis(s1,a)/P+dis(a,b)/R+dis(b,t2)/Q;
	ans=min(ans,tmp);
	return tmp;
}
double check(point S)
{
	point l=s2,r=t2,m1,m2;
	for(int i=1;i<=50;i++)
	{
		m1=(l*(2.0/3))+(r*(1.0/3)),m2=(l*(1.0/3))+(r*(2.0/3));
		if(calc(S,m1)<calc(S,m2))	r=m2;
		else	l=m1;
	}
	return calc(S,l);
}
int main()
{
	scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",&s1.x,&s1.y,&t1.x,&t1.y,&s2.x,&s2.y,&t2.x,&t2.y,&P,&Q,&R);
	point l=s1,r=t1,m1,m2;
	ans=1e10;
	for(int i=1;i<=50;i++)
	{
		m1=(l*(2.0/3))+(r*(1.0/3)),m2=(l*(1.0/3))+(r*(2.0/3));
		if(check(m1)<check(m2))	r=m2;
		else	l=m1;
	}
	printf("%.2lf",ans);
	return 0;
}//0 0 100 0 100 100 0 100 2 2 1

 

posted @ 2017-10-29 10:10  CQzhangyu  阅读(220)  评论(0编辑  收藏  举报