[HDU3400]三分求極值

Line belt

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


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
 

Author
lxhgww&&momodi
 

Source




題解轉自:http://jinvasshole.iteye.com/blog/1448590


二分法作爲分治中最常見的方法,適用於單調函數,逼近求解某點的值。但當函數是凸性函數時,二分法就無法適用,這時三分法就可以“大顯身手”~~

類似於二分的定義Left和Right,mid = (Left + Right) / 2,midmid = (mid + Right) / 2; 如果mid靠近極值點,則Right = midmid;否則(即midmid靠近極值點),則Left = mid;


三分版

double Calc(Type a)
{
    /* 根據題目的意思計算 */
}
void Solve(void)
{
    double Left, Right;
    double mid, midmid;
    double mid_value, midmid_value;
    Left = MIN; Right = MAX;
    while (Left + EPS < Right)
    {
        mid = (Left + Right) / 2;
        midmid = (mid + Right) / 2;
        mid_area = Calc(mid);
        midmid_area = Calc(midmid);
        // 假設求解最大極值.
        if (mid_area >= midmid_area) Right = midmid;
        else Left = mid;
    }
}

t=x/p+y/q+z/r

f(t)=x/p+y/q+z/r;

f(t)=x/p+g(y),g(y)=y/q+z/r;

則g(y)關於y爲凸性函數,f(t)關於x,y爲凸性函數(可能爲遞增函數),所以用兩次三分,先在外層取mid,midmid,再每次以mid,midmid爲m點對內層用三分求解最小時間g(y),完成外層的三分求解。

此外,這一題要的是對時間的精度,所以精度控制應該爲fabs(t1-t2)<eps,而不是right-left<eps,否則可能出現right-left=1e-7,但對應fabs(t1-t2)=0.1的情況!

#include <cstdio>
#include <cmath>
using namespace std;
const double eps=1e-6;
double p,q,r;
struct point{
    double x,y;
}a,b,c,d;
double dis(point a, point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
point get_mid(point a,point b){
    a.x=(a.x+b.x)/2,a.y=(a.y+b.y)/2;    return a;
}
double triple_cd(point c, point d, point m)
{
    point mid,midmid;
    point left=c,right=d; 
    double t1,t2;
    do{
        mid=get_mid(left,right);
        midmid=get_mid(mid,right);
        t1=dis(m,mid)/r+dis(d,mid)/q;
        t2=dis(m,midmid)/r+dis(d,midmid)/q;
        t1<t2?right=midmid:left=mid;
    }while(fabs(t1-t2)>=eps);
    return t1;
}    
double triple_ab(point a,point b,point c,point d)
{
    point mid,midmid,left=a,right=b;
    double t1,t2;
    left=a;
    right=b;
    do{
        mid=get_mid(left,right);
        midmid=get_mid(mid,right);
        t1=dis(mid,a)/p+triple_cd(c,d,mid);
        t2=dis(midmid,a)/p+triple_cd(c,d,midmid);
        t1<t2?right=midmid:left=mid;
    }while(fabs(t1-t2)>=eps);
    return t1;
}
int main()
{
    int t;
    scanf("%d",&t);
    double ans;
    while (t--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y,&p,&q,&r);
        ans=triple_ab(a,b,c,d);
        printf("%.2lf\n",ans);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章