hdu Line belt

这道题是一道3分搜索的题。其实这种题很多时候都出现在高中的解析几何上,思路很简单,从图中可以看到,肯定在AB线段和CD线段上各存在一点x和y使得所花时间最少
因为AB和CD上的时间与x和y点的坐标都存在一个凸函数的关系,所以可以想到利用3分搜索的方式进行求解。当然这里要用到两个三分搜索的嵌套,锁定x后找到满足条件的y,知道最小的满足条件的x和y。用三分搜索的好处就是
可以利用两点的中点坐标公式,这样比用存数学公式求解要方便的多。
#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"cmath"
#define mx 1005
#define exp 1e-6
using namespace std;
double p,q,r;
struct point
{
double x,y;
};
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 find2(point x,point c,point d)
{
point left,right,mid,midmid;
double t1,t2;
left=c;
right=d;
do
{
mid.x=(left.x+right.x)/;
mid.y=(left.y+right.y)/;
midmid.x=(mid.x+right.x)/;
midmid.y=(mid.y+right.y)/;
t1=dist(x,mid)/r+dist(mid,d)/q;
t2=dist(x,midmid)/r+dist(midmid,d)/q;
if(t1>t2) left=mid;
else right=midmid;
}while(abs(t1-t2)>exp);
return t1;
}
double find1(point a,point b,point c,point d)
{
point left,right,mid,midmid;
double t1,t2;
left=a;
right=b;
do //先循环,这样可以不用给t1,t2赋初值
{
mid.x=(left.x+right.x)/;
mid.y=(left.y+right.y)/;
midmid.x=(mid.x+right.x)/;
midmid.y=(mid.y+right.y)/;
t1=dist(a,mid)/p+find2(mid,c,d);
t2=dist(a,midmid)/p+find2(midmid,c,d);
if(t1>t2) left=mid;
else right=midmid;
}while(abs(t1-t2)>exp);
return t1;
}
int main()
{
int t;
cin>>t;
point a,b,c,d;
while(t--)
{
cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y;
cin>>p>>q>>r;
printf("%.2lf\n",find1(a,b,c,d));
}
return ;
}
hdu 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 (三分再三分)
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 ...
- HDU 3400 Line belt (三分嵌套)
题目链接 Line belt Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Line belt
Problem Description In a two-dimensional plane there are two line belts, there are two segments AB a ...
- HDU 3400 Line belt (三分套三分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3400 题意: 有两条带子ab和cd,在ab上的速度为p,在cd上的速度为q,在其它地方的速度为r.现 ...
- HDU 3400 Line belt【三分套三分】
从A出发到D,必定有从AB某个点E出发,从某个点F进入CD 故有E,F两个不确定的值. 在AB上行走的时间 f = AE / p 在其他区域行走的时间 g = EF / r 在CD上行走的时间 ...
- hdu 3400 Line belt 三分法
思路:要求最短时间从A到D,则走的路线一定是AB上的一段,CD上的一段,AB与CD之间的一段. 那么可以先三分得到AB上的一个点,在由这个点三分CD!! 代码如下: #include<iostr ...
- hdu 3400 Line belt
题意:给你两条线段AB,CD:然后给你在AB,CD上的速度P,Q,在其它部分的速度是R,然后求A到D的最短时间. 思路:用三分枚举从AB线段上离开的点,然后再用三分枚举在CD的上的点找到最优点,求距离 ...
随机推荐
- Java读写文件的几种方式
自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...
- iOS 利用self.navigationItem.backBarButtonItem修改后退按钮文字
@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem; // Bar button item to use for the ba ...
- HDU1695 GCD (欧拉函数+容斥原理)
F - GCD Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- plsql查询数据显示为乱码解决方法
使用plsql查询数据显示为乱码: 查看数据库编码: 通过网上搜索,发现需要设置环境变量,添加以下环境变量: LANG=zh_CN.GBK NLS_LANG="SIMPLIFIED CHIN ...
- python基础——使用list和tuple
python基础——使用list和tuple list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用 ...
- Binary Search--二分查找
Binary Search--二分查找 采用二分法查找时,数据需是排好序的. 基本思想:假设数据是按升序排序的,对于给定值x,从序列的中间位置开始比较,如果当前位置值等于x,则查找成功:若x小于当前位 ...
- Android数据存储之sharedpreferences与Content Provider
android中对数据操作包含有: file, sqlite3, Preferences, ContectResolver与ContentProvider前三种数据操作方式都只是针对本应用内数据,程序 ...
- 对线程调度中Thread.sleep(0)的深入理解
在Java或者C#中,都会用到 Thread.Sleep()来使线程挂起一段时间.那么你有没有正确的理解这个方法的用法呢?思考下面这两个问题: 1.假设现在是 2014-8-13 17:00:00.0 ...
- 让Entity Framework支持MySql数据库(转载)
转载地址:http://www.cnblogs.com/wintersun/archive/2010/12/12/1903861.html Entity Framework 4.0 也可以支持大名鼎鼎 ...
- 【ACM - 搜索模板】
[广搜模板] #include <iostream> #include <stdio.h> #include <string.h> #include <que ...