DescriptionAsakura存在于一个魔法世界中。有一天,Asakura在一条魔法通道里偷懒,突然接到一个紧急任务,要快速赶往另一条通道b去。 我们把通道a和b看作两条线段AB和CD,Asakura初始位置在A,现在要快速赶往D。Asakura在魔法通道a上的速度为v1,在魔法通道b上速度为v2,在除了这两条通道上的其余位置的速度为v3。Asakura最快多长时间才能到达指定位置。 Input多组输入 对于每组测试数据:包含三行 第一行输入四个整数x1,y1,x2,y2.其中(x1,y1)为A,(x2,y2)为B.(-10000<=x1,y1,x2,y2<=10000) 第二行输入四个整数x3,y3,x4,y4.其中(x3,y3)为C,(x4,y4)为D.(-10000<=x3,y3,x4,y4<=10000) 第三行输入三个整数v1,v2,v3.(1<=v1,v2,v3<=50) 每两组测试数据间输入一个空行。 Output输出A到D最短的时间,结果精度到小数点后两位(四舍五入)。 Sample Input0 0 50 50 50 0 50 50 1 1 1 0 0 50 50 50 0 50 50 3 1 3 0 0 50 50 50 0 50 50 1 3 1 Sample Output70.71 23.57 63.81
代码如下: #include<cstdio> #include<cmath> double dis( double
x1, double
y1, double x2,
double y2)
{ return sqrt ((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)); } double v1, v2, v3, x[4], y[4];
double mindis( double xi,
double yi)
{ double i = x[3] - x[2];
double j = y[3] - y[2];
double l = 0, r = 1;
int t;
double tmid, tmidmid;
for (t = 1; t <= 50; t++)
{ double mid = (l + r) / 2;
double midmid = (mid + r) / 2;
double midx = x[2] + mid*i;
double midy = y[2] + mid*j;
double midmidx = x[2] + midmid*i;
double midmidy = y[2] + midmid*j;
tmid = dis(xi, yi, midx, midy) / v3 + dis(midx, midy, x[3], y[3]) / v2; tmidmid = dis(xi, yi, midmidx, midmidy) / v3 + dis(midmidx, midmidy, x[3], y[3]) / v2; if (tmid > tmidmid)
l = mid; else r = midmid;
} return tmidmid;
} int
main() { while (~ scanf ( "%lf
%lf %lf %lf" , &x[0], &y[0], &x[1], &y[1])) { scanf ( "%lf %lf %lf %lf" , &x[2], &y[2], &x[3], &y[3]); scanf ( "%lf %lf %lf" , &v1, &v2, &v3); double i = x[1] - x[0];
double j = y[1] - y[0];
double l = 0, r = 1;
int t;
double tmid, tmidmid;
for (t = 1; t <= 50; t++)
{ double mid = (l + r) / 2;
double midmid = (mid + r) / 2;
double midx = x[0] + mid*i;
double midy = y[0] + mid*j;
double midmidx = x[0] + midmid*i;
double midmidy = y[0] + midmid*j;
tmid = dis(x[0], y[0], midx, midy) / v1 + mindis(midx, midy); tmidmid = dis(x[0], y[0], midmidx, midmidy) / v1 + mindis(midmidx, midmidy); if (tmid > tmidmid)
l = mid; else r = midmid;
} printf ( "%.2lf\n" , tmid); } return 0;
} |
原文地址:http://blog.csdn.net/wxq_wuxingquan/article/details/26108871