传送带

题目描述

思路

三角形两边之和大于第三边

代码

#include <cstdio>
#include <cmath>
using namespace std;
struct Node {
    double x, y;
} a, b, c, d;
int p, q, w;
double getTime(Node a, Node b, int c) {
    return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)) / c;
}
double cd(Node z) {
    Node l, r, midl, midr;
    double  ansl, ansr, ans;
    l = c, r = d;
    while (getTime(l, r, 1) > 1e-4) {
        midl.x = l.x + (r.x - l.x) / 3.0;
        midl.y = l.y + (r.y - l.y) / 3.0;
        midr.x = r.x - (r.x - l.x) / 3.0;
        midr.y = r.y - (r.y - l.y) / 3.0;
        ansl = getTime(z, midl, w) + getTime(midl, d, q);
        ansr = getTime(z, midr, w) + getTime(midr, d, q);
        if (ansl < ansr) ans = ansl, r = midr;
        else ans = ansr, l = midl;
    }
    return ans;
}
double ab() {
    Node l, r, midl, midr;
    double ansl, ansr, ans;
    l = a, r = b;
    while (getTime(l, r, 1) > 1e-4) {  //两个点之间还有距离的时候
        midl.x = l.x + (r.x - l.x) / 3.0;
        midl.y = l.y + (r.y - l.y) / 3.0;
        midr.x = r.x - (r.x - l.x) / 3.0;
        midr.y = r.y - (r.y - l.y) / 3.0;
        ansl = cd(midl) + getTime(midl, a, p);
        ansr = cd(midr) + getTime(midr, a, p);
        if (ansl < ansr) ans = ansl, r = midr;
        else ans = ansr, l = midl;
    }
    return cd(l) + getTime(l, a, p); //解决a,b是同一个点的情况
}

int main() {
    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("%d %d %d", &p, &q, &w);
    printf("%.2lf", ab());
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/liuzz-20180701/p/11583920.html