hdoj 3400 Line belt (三分嵌套搜索 )

来源:互联网 发布:jsp时间轴数据库读取 编辑:程序博客网 时间:2024/06/01 09:06

Line belt

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


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
10 0 0 100100 0 100 1002 2 1
 

Sample Output
136.60
 

AB线段上找一点p,CD线段上找一点q,使Ap-->pq-->qD路线的耗时最短。

直接写函数写不出来,由于是找最小值,三分搜索在两条线段上无限逼近答案p,q 两点。

code

#include <iostream>#include<cstdio>#include <cmath>using namespace std;#define eps 1e-10;struct Node{    double x,y;};Node A,B,C,D;double Q,P,R;double cost(Node a,Node b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double f(Node p,Node c,Node d){    Node l,r,mid,midmid;    l=c;r=d;    while(cost(l,r)>=1e-10)    {        mid.x=(l.x+r.x)/2;        mid.y=(l.y+r.y)/2;        midmid.x=(mid.x+r.x)/2;        midmid.y=(mid.y+r.y)/2;        if(cost(p,mid)/R+cost(mid,d)/Q<cost(p,midmid)/R+cost(midmid,d)/Q)            r=midmid;        else            l=mid;    }    return cost(p,mid)/R+cost(mid,d)/Q;}int main(){    int n;    scanf("%d",&n);    while(n--)    {        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);        Node l,r,mid,midmid;        l=A;r=B;        while(cost(l,r)>=1e-10)        {            mid.x=(l.x+r.x)/2;            mid.y=(l.y+r.y)/2;            midmid.x=(mid.x+r.x)/2;            midmid.y=(mid.y+r.y)/2;            if(cost(A,mid)/P+f(mid,C,D)<cost(A,midmid)/P+f(midmid,C,D))//三分的嵌套                r=midmid;            else                l=mid;        }        printf("%.2lf\n",cost(A,mid)/P+f(mid,C,D));    }    return 0;}


0 0
原创粉丝点击