HDU 3400 Line belt 【三分嵌套】

 

In a two-dimensional plane there are two line belts, there are two segments AB and CD, lxhgww's speed on AB is P and on CD is Q, he can move with the speed R on other area on the plane. 
How long must he take to travel from A to D?

InputThe first line is the case number T. 
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<=10OutputThe minimum time to travel from A to D, round to two decimals.Sample Input

1
0 0 0 100
100 0 100 100
2 2 1

Sample Output

136.60

 

 

 

题意:

在AB,CD两条线段,求从A点到D点的最小距离,原先是思路是通过三分的方法找出两条线段对应的 0 <= 

 λ <= 1  使得所走的距离最短;但是,在三分的过程中不知道怎么去处理当
 λ=0的情况,如果等于零的话,所求的线段长度是不准确的,这里可以三分去找出AB内的点和CD内的点,通过点与点直接求线段的距离,用struct来存储x和y的坐标,这里的三分嵌套就是,先给AB三分,然后在AB的基础上再给CD三分,下面给出代码:

 

 

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<time.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<list>
#include<queue>
#include<map>
#define E exp(1)
#define PI acos(-1)
#define mod (ll)(1e9+9)
#define INF 0x3f3f3f3f;
#define MAX 40000
#define compare 0.00000001
//#define _CRT_SECURE_NO_WARNINGS
//#define LOCAL
using namespace std;

typedef long long ll;
typedef long double lb;

double Ax, Bx, Ay, By, Cx, Cy, Dx, Dy, p, q, r;
struct point {
	double x;
	double y;
};

struct point A, B, C, D;
   //m 为AB内的点的坐标,mm为CD内的点的坐标
double formula(struct point mm,struct point m) {
	double ans_1 = sqrt(pow(D.x - mm.x, 2) + pow(D.y - mm.y, 2)) / q;
	double ans_2 = sqrt(pow(mm.x - m.x, 2) + pow(mm.y - m.y, 2)) / r;
	double ans_3 = sqrt(pow(A.x - m.x, 2) + pow(A.y - m.y, 2)) / p;
	return ans_1 + ans_2 + ans_3;
}
		//m 为AB内的点的坐标
double devide(struct point m) {
	struct point lle, rri, llmid, rrmid;
	lle = C, rri = D;
	while (sqrt(pow(lle.x - rri.x, 2) + pow(lle.y - rri.y, 2)) > 1e-8) {
		llmid.x = (lle.x + rri.x) / 2;
		llmid.y = (lle.y + rri.y) / 2;
		rrmid.x = (llmid.x + rri.x) / 2;
		rrmid.y = (llmid.y + rri.y) / 2;
		if (formula(llmid,m) > formula(rrmid,m)) lle = llmid;
		else rri = rrmid;
	}
	return formula(rri, m);
}

int main(void)
{
#ifdef LOCAL
	freopen("data.in.txt", "r", stdin);
	freopen("data.out.txt", "w", stdout);
#endif
	int t;
	while (scanf("%d", &t) != EOF) {
		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);

			struct point le, ri, lmid, rmid;
			le = A, ri = B;
			while (sqrt(pow(le.x - ri.x, 2) + pow(le.y - ri.y, 2)) > 1e-8) {
				lmid.x = (le.x + ri.x) / 2;
				lmid.y = (le.y + ri.y) / 2;
				rmid.x = (lmid.x + ri.x) / 2;
				rmid.y = (lmid.y + ri.y) / 2;
				if (devide(lmid) > devide(rmid)) le = lmid;
				else ri = rmid;
			}
			double res = devide(ri);
			printf("%.2f\n", res);
		}
	}
	//	end = clock();
	//	cout << "using tmie:" << (double)(end - start) / CLOCKS_PER_SEC * (1000) << "ms" << endl;

	//system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值