HDU OJ 3400 Line belt

该博客主要讨论了一道计算机算法问题,即在二维平面上,一个人以不同速度在两条线段AB和CD上行走,寻找从点A到点D的最短时间。博主使用了三分法策略来确定路径上的关键点,通过计算每个可能点的时间来逐步逼近最短时间。提供的C++代码展示了如何实现这个算法,包括找到线段AB和CD上的最佳中间点,以最小化总旅行时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

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?

输入

The 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<=10

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

输出

The minimum time to travel from A to D, round to two decimals.

136.60

解题思路

  • 问题要求从A到D点所需的最短时间。显然在AB段必存在一点E,CD段存在一点F,使得从A到D时间最短。
    在这里插入图片描述
  • 先用三分法确定一个E点,然后E点可看作一个固定点。
  • 随后用三分法确定F点,算出最短时间。

c++代码

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

double bias = 1e-8;
int Ax, Ay, Bx, By, Cx, Cy, Dx, Dy;
int speedAB, speedOther, speedCD;

double getCDPoint(double X, double Y){ 
    double startX = Cx, startY = Cy, endX = Dx, endY = Dy, mid1X, mid1Y, mid2X, mid2Y;
    double time1 = 0, time2 = 0;
    bool flag = true;
    while(flag){
        mid1X = startX + (endX - startX) / 3;
        mid1Y = startY + (endY - startY) / 3;
        mid2X = endX - (endX - startX) / 3;
        mid2Y = endY - (endY - startY) /3;
        time1 = (sqrt(pow((Dx - mid1X), 2) + pow((Dy - mid1Y), 2)) / speedCD) + (sqrt(pow((X - mid1X), 2) + pow((Y - mid1Y), 2)) / speedOther);
        time2 = (sqrt(pow((Dx - mid2X), 2) + pow((Dy - mid2Y), 2)) / speedCD) + (sqrt(pow((X - mid2X), 2) + pow((Y - mid2Y), 2)) / speedOther);
        if(time1 < time2){
            endX = mid2X;
            endY = mid2Y;
        }
        else{
            startX = mid1X;
            startY = mid1Y;
        }
        if(abs(time1 - time2) <= bias){
            flag = false;
        }
    }
    return time1;
}

double getABPoint(){
    double startX = Ax, startY = Ay, endX = Bx, endY = By, mid1X, mid1Y, mid2X, mid2Y;
    double time1 = 0, time2 = 0;
    bool flag = true;
    while(flag){
        mid1X = startX + (endX - startX) / 3;
        mid1Y = startY + (endY - startY) / 3;
        mid2X = endX - (endX - startX) / 3;
        mid2Y = endY - (endY - startY) /3;
        time1 = sqrt(pow((mid1X - Ax), 2) + pow((mid1Y - Ay), 2)) / speedAB + getCDPoint(mid1X, mid1Y);
        time2 = sqrt(pow((mid2X - Ax), 2) + pow((mid2Y - Ay), 2)) / speedAB + getCDPoint(mid2X, mid2Y);
        if(time1 < time2){
            endX = mid2X;
            endY = mid2Y;
        }
        else{
            startX = mid1X;
            startY = mid1Y;
        }
        if(abs(time1 - time2) <= bias){
            flag = false;
        }
    }
    return time1;
}

int main(){
    int numCases;
    cin >> numCases;
    while (numCases--)
    {
        cin >> Ax >> Ay >> Bx >> By >> Cx >> Cy >> Dx >> Dy;
        cin >> speedAB >> speedCD >> speedOther;
        cout << fixed << setprecision(2) << getABPoint() << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值