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.<br>How long must he take to travel from A to D?
Input
The first line is the case number T.<br>For each case, there are three lines.<br>The first line, four integers, the coordinates of A and B: Ax Ay Bx By.<br>The second line , four integers, the coordinates of C and D:Cx Cy Dx Dy.<br>The third line, three integers, P Q R.<br>0<= Ax,Ay,Bx,By,Cx,Cy,Dx,Dy<=1000<br>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,要求从A点走到D点,则先在AB上走一段,然后在两线段之间走一段,再上CD上走一段,三个地方的速度不同,求最短时间。
解题思路:
看懂题意后明白了应使用三分法,,然后就不明白了,AB上的点影响着空白处的长度,空白处的长度又和CD上的点的选取有关,两边同时使用三分法又不明白代码怎么实现,然后 懵掉之后就没再看,看了大佬的题解,才明白了两个三分是如何实现的,三分枚举AB上的点,或者 时间,对应于这个点或者这个时间,去三分CD段,找到对应AB上当前点 的 使整体时间最小的BC上的点,然后在AB上挪一下,BC上搜一遍,这样子挪挪挪,找到那个使整体最小的点
(很悲催的我的代码死活不过,找不到Bug又看的大佬的题解才过了)
下面是代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps=1e-6;
struct node//每一个坐标的x,y;
{
double x;
double y;
}a,b,c,d,bb,dd;
double ab,cd;
double p,q,r;
double dis(node u,node v)//u点和v点之间的差距
{
return sqrt(eps+(u.x-v.x)*(u.x-v.x)+(u.y-v.y)*(u.y-v.y));
}
double work(double t)
{
dd.x=d.x+(c.x-d.x)/cd*t*q;
dd.y=d.y+(c.y-d.y)/cd*t*q;
return t+dis(bb,dd)/r;//中间那一节的时间
}
double solve(double t)//t代表一个时间点,AB上的
{
bb.x=a.x+(b.x-a.x)/ab*t*p;//带一个点试试,比方说时间就为ab/p,或者ab/2p
bb.y=a.y+(b.y-a.y)/ab*t*p;
double l=0,h=cd/q,mid1,mid2;
while(l+eps<h)
{
mid1=l+(h-l)/3;
mid2=h-(h-l)/3;
if(work(mid1)<work(mid2))
{
h=mid2;
}
else
l=mid1;
}
return t+work(l);//找到AB上定点时的最小时间
}
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("%lf%lf%lf",&p,&q,&r);//p,q,r,AB上是p,CD上是q,空地上是r
ab=dis(a,b);//AB的长度
cd=dis(c,d);//CD的长度
double l=0.0,h=ab/p,//h=AB上以速度p所用的总时间
mid1,mid2;
while(l+eps<h)
{
mid1=l+(h-l)/3;
mid2=h-(h-l)/3;
if(solve(mid1)<solve(mid2))
h=mid2;
else
l=mid1;
}
printf("%.2f\n",solve(mid1));
}
return 0;
}
//下方即为这段失败的bug
//#include<iostream>
//#include<stdio.h>
//#include<math.h>
//using namespace std;
//const double eps=1e-6;
//struct aim
//{
// double x;
// double y;
//};
//aim a,b,c,d,bb,dd;//bb=ab上的移动的点;dd=cd上的移动的点
//double ab,cd;
//double p,q,r;//ab上,cd上,空地上速度
//double dis(aim u,aim v)//distance
//{
// double dx=u.x-v.x;
// double dy=u.y-v.y;
// return sqrt(dx*dx+dy*dy);
// //return sqrt(eps+(u.x-v.x)*(u.x-v.x)+(u.y-v.y)*(u.y-v.y));
//}
//double plane(double ti)//ti=time ,由时间推出点dd在cd上的位置坐标
//{
// dd.x=d.x+(c.x-d.x)/cd*ti*q;
// dd.y=d.y+(c.y-d.y)/cd*ti*q;
// return ti+dis(bb,dd)/r;
//}
//double solve(double t)
//{
// bb.x=a.x+(b.x-a.x)/ab*t*p;//带一个点试试,比方说时间就为ab/p,或者ab/2p
// bb.y=a.y+(b.y-a.y)/ab*t*p;
// double l=0,h=cd/q,mid1,mid2;//同ab上的点,不过要带上空白处的时间
// while(l+eps<h)
// {
// mid1=l+(h-l)/3;
// mid2=h-(h-l)/3;
// if(plane(mid1)<plane(mid2))
// {
// h=mid2;
// }
// else
// l=mid1;
// }
// return t+plane(l);//找到AB上定点时的最小时间
//}
//int main()
//{
// int tt;
// scanf("%d",&tt);
// while(tt--)
// {
// 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("%lf%lf%lf",&p,&q,&r);//ab上,cd上,空地上速度
// ab=dis(a,b);//线段的长度
// cd=dis(c,d);
// double low=0.0,high=ab/p;
// double mid,mmid;
// while(high-low>eps)
// {
// mid=low+(high-low)/3;
// mmid=high-(high-low)/3;
// if(solve(mid)>=solve(mmid))
// low=mid;
// else
// high=mmid;
// }
// printf("%0.2lf\n",solve(mid));
// }
// return 0;
//}