[3] HDU 3400

Source: Internet
Author: User

Two parallel lines, AB, CD, and AB, are given. The speed of CD is Q, and the line segment is R. How long is the shortest time from A to D?

Method: 3 points. After a simple analysis, we found that this function is a single-peak function, which is first reduced and then increased. It is applicable to three points;

Implementation: first, 3-points for AB, nested with 3-points for CD;

#include <list>#include <map>#include <set>#include <queue>#include <string>#include <deque>#include <stack>#include <algorithm>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <limits.h>#include <time.h>#include <string.h>using namespace std;#define LL long long#define PI acos(-1.0)#define MAX INT_MAX#define MIN INT_MIN#define eps 1e-10#define FRE freopen("a.txt","r",stdin)#define N 1000struct point{    double x,y;};double p,q,r;double dis(point a,point b){    return sqrt( (a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y) );}double cal_cd(point c,point d,point mm){    int i,j;    point ll,rr,mid,midmid;    ll=c;    rr=d;    double t1,t2;    do{        mid.x=(ll.x+rr.x)/2.0;        mid.y=(ll.y+rr.y)/2.0;        midmid.x=(mid.x+rr.x)/2.0;        midmid.y=(mid.y+rr.y)/2.0;        t1=dis(d,mid)/q+dis(mid,mm)/r;        t2=dis(d,midmid)/q+dis(midmid,mm)/r;        if(t1<t2)        rr=midmid;        else        ll=mid;    }while(fabs(t1-t2)>=eps);    return t1;}double cal_ab(point a,point b,point c,point d){    int i,j;    point ll,rr,mid,midmid;    ll=a;    rr=b;    double t1,t2;    do{        mid.x=(ll.x+rr.x)/2.0;        mid.y=(ll.y+rr.y)/2.0;        midmid.x=(mid.x+rr.x)/2.0;        midmid.y=(mid.y+rr.y)/2.0;        double tmp1=cal_cd(c,d,mid);        double tmp2=cal_cd(c,d,midmid);        t1=dis(a,mid)/p+tmp1;        t2=dis(a,midmid)/p+tmp2;        if(t1<t2)           rr=midmid;        else        ll=mid;    }while(fabs(t1-t2)>=eps);    return t1;}int main(){    int t;    scanf("%d",&t);    while(t--){        point a,b,c,d;        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 ans=cal_ab(a,b,c,d);        printf("%.2f\n",ans);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.