hdu 3400(三分)
2016-05-27 21:00
351 查看
题目链接:点击打开链接;
题意:本题给出两条线段,一条线段端点为a,b;另一条线段端点是c,d,在两条线段和不在线段上有不同的速度,求最短时间。
分析:本题一共有三段时间,在a-b上,在c-d上,在空白的地方。设着三段时间是x,y,z。则一旦x和y确定之后,z就能确定,因为这样在两线段上的点就确定了,z也就求出来了,如果x确定时y不确定的话,z和y有关系,所以时间是有一个极值,想到极值,就应该三分求最适合的y。而对于x来说,三分求解x的话可以搞出对应点的y的极值,也就能求出队应的y的极值。这是就解出来了。
代码如下:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps=1e-7;
const double ee=1e-9;
struct Point{
double x,y;
};
double dis(Point a,Point b){
return sqrt(ee+(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double q,p,r;
Point a,b,c,d,tmpx,tmpy;
double call(double t){
tmpy.x=d.x+(c.x-d.x)/dis(c,d)*t*q;
tmpy.y=d.y+(c.y-d.y)/dis(c,d)*t*q;
return t+dis(tmpx,tmpy)/r;
}
double sanfen(double t){
tmpx.x=a.x+(b.x-a.x)/dis(a,b)*t*p;
tmpx.y=a.y+(b.y-a.y)/dis(a,b)*t*p;
double low,high,mid,midd;
low=0;
high=dis(c,d)/q;
while(high-low>eps){
mid=(low+high)/2;
midd=(mid+high)/2;
if(call(mid)<call(midd))high=midd;
else low=mid;
}
return t+call(mid);
} //三分求y值
int main(){
int t;
scanf("%d",&t);
while(t--){
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 low,high,mid,midd;
low=0;
high=dis(a,b)/p;
while(high-low>eps){
mid=(low+high)/2;
midd=(mid+high)/2;
if(sanfen(mid)<sanfen(midd))high=midd;
else low=mid;
} //三分求x的值
printf("%.2f\n",sanfen(mid));
}
return 0;
}
题意:本题给出两条线段,一条线段端点为a,b;另一条线段端点是c,d,在两条线段和不在线段上有不同的速度,求最短时间。
分析:本题一共有三段时间,在a-b上,在c-d上,在空白的地方。设着三段时间是x,y,z。则一旦x和y确定之后,z就能确定,因为这样在两线段上的点就确定了,z也就求出来了,如果x确定时y不确定的话,z和y有关系,所以时间是有一个极值,想到极值,就应该三分求最适合的y。而对于x来说,三分求解x的话可以搞出对应点的y的极值,也就能求出队应的y的极值。这是就解出来了。
代码如下:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps=1e-7;
const double ee=1e-9;
struct Point{
double x,y;
};
double dis(Point a,Point b){
return sqrt(ee+(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double q,p,r;
Point a,b,c,d,tmpx,tmpy;
double call(double t){
tmpy.x=d.x+(c.x-d.x)/dis(c,d)*t*q;
tmpy.y=d.y+(c.y-d.y)/dis(c,d)*t*q;
return t+dis(tmpx,tmpy)/r;
}
double sanfen(double t){
tmpx.x=a.x+(b.x-a.x)/dis(a,b)*t*p;
tmpx.y=a.y+(b.y-a.y)/dis(a,b)*t*p;
double low,high,mid,midd;
low=0;
high=dis(c,d)/q;
while(high-low>eps){
mid=(low+high)/2;
midd=(mid+high)/2;
if(call(mid)<call(midd))high=midd;
else low=mid;
}
return t+call(mid);
} //三分求y值
int main(){
int t;
scanf("%d",&t);
while(t--){
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 low,high,mid,midd;
low=0;
high=dis(a,b)/p;
while(high-low>eps){
mid=(low+high)/2;
midd=(mid+high)/2;
if(sanfen(mid)<sanfen(midd))high=midd;
else low=mid;
} //三分求x的值
printf("%.2f\n",sanfen(mid));
}
return 0;
}
相关文章推荐
- 初学算法 - 求凸包的Garham's Scan算法的C++实现
- 【Google Code Jam 2009 round2 problem D】Watering Plants (两圆交点求法详解)
- 计算几何模板
- 计算几何小模板
- BZOJ2829信用卡凸包
- HDU4355--Party All the Time,HDU3714--Error Curvese三分法
- CSU 1548 Road And Bridge acm 三分
- HDU 4717 The Moving Points
- HDU 4922 Hello, Your Package! (计算几何+DP)(WA)
- poj 1514&zoj 1185 Metal Cutting(半平面交)
- UVA 10969 Sweet Dream(圆的相交)
- uva 11177 Fighting Against a Polygonal Monster(凸包与圆的面积交)
- POJ1279 && LA2512 Art Gallery(求多边形的核)
- poj 2540 && uva 10084 Hotter Colder(半平面交)
- UVALive 5878 - Shortest Leash 【计算几何】
- 【计算几何】POJ 2318 & POJ 2398
- 【计算几何】POJ 2653
- 【计算几何】POJ 1113
- HDU 5128 The E-pang Palace
- POJ 2318 TOYS(叉积+二分or暴力)