第一次写三分囧。
如果求一个点到一条线段的最短距离,可以在线段上三分,然后我也不知道这题为什么能三分囧。。网上都说是三分而且网上也没有证明。。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#define eps 1e-8
using namespace std;
struct node {double x,y;};
node a,b,c,d;
double p,q,r;
node cal(node a,node b,double p)
{
node ans;
ans.x=a.x+(b.x-a.x)*p;
ans.y=a.y+(b.y-a.y)*p;
return ans;
}
double dis(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double solve2(node x,node y)
{
return dis(a,x)/p+dis(x,y)/r+dis(y,d)/q;
}
double solve1(node t)
{
double l=0.0,r=1.0,ans;
while (fabs(r-l)>eps)
{
double mid=(l+r)/2.0,midmid=(mid+r)/2.0;
node x=cal(c,d,mid),y=cal(c,d,midmid);
if (solve2(t,x)<solve2(t,y)) ans=mid,r=midmid; else ans=midmid,l=mid;
}
node x=cal(c,d,ans);
return solve2(t,x);
}
int main()
{
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=0.0,r=1.0,ans;
while (fabs(r-l)>eps)
{
double mid=(l+r)/2.0,midmid=(mid+r)/2.0;
node x=cal(a,b,mid),y=cal(a,b,midmid);
if (solve1(x)<solve1(y)) ans=mid,r=midmid; else ans=midmid,l=mid;
}
node x=cal(a,b,ans);
printf("%.2lf\n",solve1(x));
return 0;
}