HDOJ 3400 三分法
1Y。只是不明白為什么可以套兩個三分,不過從實際情況來看,在第一條直線上三分應該也是符合凸函數性質的。
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define eps 1e-8

struct point

{
double x,y;
};
point a,b,c,d;
double p,q,r;
double dist(point a,point b)

{
return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
}
double cal(double t1,double t2)//以t1,t2為參數算出時間

{
point tm1;
point tm2;
tm1.x=a.x+(b.x-a.x)*t1;
tm1.y=a.y+(b.y-a.y)*t1;
tm2.x=c.x+(d.x-c.x)*t2;
tm2.y=c.y+(d.y-c.y)*t2;

return dist(tm1,a)/p+dist(tm2,d)/q+dist(tm1,tm2)/r;
}

double sanfen(double t1)//在確定t1的基礎上得最小值

{
double l=0;
double r=1;
while(l+eps<=r)
{
double mid=(l+r)/2;
double mmid=(mid+r)/2;
if(cal(t1,mid)<cal(t1,mmid))
r=mmid;
else
l=mid;
}
return cal(t1,l);
}


int main()

{
int ca;
scanf("%d",&ca);
while(ca--)
{
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);
double l,r;
l=0;r=1;
while(l+eps<=r)
{
double mid=(l+r)/2;
double mmid=(mid+r)/2;
if(sanfen(mid)<sanfen(mmid))
r=mmid;
else
l=mid;
}
printf("%.2lf\n",sanfen(l));
}
return 0;
}posted on 2010-11-07 20:57 abilitytao 閱讀(330) 評論(0) 編輯 收藏 引用

