Line belt(三分镶嵌)
How long must he take to travel from A to D?
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
1
0 0 0 100
100 0 100 100
2 2 1
Sample Output
136.60 题目意思:就是给你两条线段AB , CD的坐标 ,一个人在AB以速度p跑,在CD上以q跑,在其他地方跑速度是r,问你从A到D最少的时间。
解题思路:设E在AB上,F在CD上。 则人在线段AB上花的时间为:f = AE / p,人走完Z和Y所花的时间为:g= EF / r + FD / q。
f函数是一个单调递增的函数,而g很明显是一个先递减后递增的函数。两个函数叠加,所得的函数应该也是一个先递减后递增的函数。这算是一道三分又三分的题目,可以看成是三分的镶嵌,
先对AB上的位置E进行三分枚举,每一次枚举的时候把E看做定点,在这种情况下,再对CD上的位置F进行三分枚举,这样可以求出此次三分AB的最小时间。然后完成对AB的三分后,就会得到从A到D的最短时间。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define EPS 1e-8
struct Point
{
double x;
double y;
} a, b, c, d, e, f;
double p, q, r;
double dis(Point p1, Point p2)///两点之间的距离
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
double calc(double alpha)///alpha代表在f点在cd中位置的比率
{
f.x = c.x + (d.x - c.x) * alpha;
f.y = c.y + (d.y - c.y) * alpha;
return dis(f, d) / q + dis(e, f) / r;///返回在ef和fd上花费的时间
}
double inter_tri(double alpha)///在f点进行三分
{
double l = 0.0, r = 1.0, mid, mmid, cost;
e.x = a.x + (b.x - a.x) * alpha;
e.y = a.y + (b.y - a.y) * alpha;///e点在线段ab中的位置
while (r - l > EPS)
{
mid = (l + r) / ;
mmid = (mid + r) / ;
cost = calc(mid);
if (cost <= calc(mmid))
r = mmid;
else
l = mid;
}
return dis(a, e) / p + cost;
}
double solve()///在e点进行三分
{
double l = 0.0, r = 1.0, mid, mmid, ret;
while (r - l > EPS)
{
mid = (l + r) / ;
mmid = (mid + r) / ;
ret = inter_tri(mid);
if (ret <= inter_tri(mmid))
r = mmid;
else
l = mid;
}
return ret;
}
int main()
{
int T;
double ans;
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);
ans=solve();
printf("%.2lf\n",ans);
}
return ;
}
Line belt(三分镶嵌)的更多相关文章
- 三分套三分 --- HDU 3400 Line belt
Line belt Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3400 Mean: 给出两条平行的线段AB, CD,然后一 ...
- HDU 3400 Line belt (三分嵌套)
题目链接 Line belt Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 3400 Line belt (三分再三分)
HDU 3400 Line belt (三分再三分) ACM 题目地址: pid=3400" target="_blank" style="color:rgb ...
- 搜索(三分):HDU 3400 Line belt
Line belt Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Line belt
Problem Description In a two-dimensional plane there are two line belts, there are two segments AB a ...
- HDU 3400 Line belt【三分套三分】
从A出发到D,必定有从AB某个点E出发,从某个点F进入CD 故有E,F两个不确定的值. 在AB上行走的时间 f = AE / p 在其他区域行走的时间 g = EF / r 在CD上行走的时间 ...
- HDU 3400 Line belt (三分套三分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3400 题意: 有两条带子ab和cd,在ab上的速度为p,在cd上的速度为q,在其它地方的速度为r.现 ...
- hdu Line belt
这道题是一道3分搜索的题.其实这种题很多时候都出现在高中的解析几何上,思路很简单,从图中可以看到,肯定在AB线段和CD线段上各存在一点x和y使得所花时间最少 因为AB和CD上的时间与x和y点的坐标都存 ...
- hdu 3400 Line belt 三分法
思路:要求最短时间从A到D,则走的路线一定是AB上的一段,CD上的一段,AB与CD之间的一段. 那么可以先三分得到AB上的一个点,在由这个点三分CD!! 代码如下: #include<iostr ...
随机推荐
- RCF的简单使用教程以及什么是回调函数
RCF的使用教程 RCF(Remote Call Framework)是一个使用C++编写的RPC框架,在底层RCF支持多种传输实现方式(transport implementations). 包括T ...
- Spring Boot 2.0 设置网站默认首页
Spring Boot设置默认首页,方法实验OK如下 附上Application启动代码 /** * @ClassName Application * @Description Spring-Boot ...
- 在centos6.7通过源码安装python3.6.7报错“zipimport.ZipImportError: can't decompress data; zlib not available”
在centos6.7通过源码安装python3.6.7报错: zipimport.ZipImportError: can't decompress data; zlib not available 从 ...
- Eclipse切换字体颜色
打开window-preferences
- JavaScript 中 Property 和 Attribute 的区别详解
property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...
- Python入门 —— 2048实战(字符界面和图形界面)
2048 game (共4种实现方法) 目录: .. 图形界面 ... pygame 和 numpy .. 字符界面 ... 第一种 ... curses ... wxpython ... 第二种 . ...
- 详解HTML5中的进度条progress元素简介及兼容性处理
一.progress元素基本了解 1.基本知识 progress元素属于HTML5家族,指进度条.IE10+以及其他靠谱浏览器都支持. 注释:Internet Explorer 9 以及更早的版本不支 ...
- Go搭建一个博客系统
go语言环境就不用多说了,版本肯定越高越好,这里用go1.10 先放着
- create a nodejs npm package
1. create a folder named m1 2. run command: npm init, this will create the package.json file 3. crea ...
- silverlight 图形报表开发
前端: <UserControl x:Class="SLThree.CharReport" xmlns="http://schemas.microsoft.com/ ...