Mindis HDU-6097

来源:互联网 发布:空气质量监测数据 编辑:程序博客网 时间:2024/06/06 01:30

题目链接:点我


The center coordinate of the circle C is O, the coordinate of O is (0,0) , and the radius is r.P and Q are two points not outside the circle, and PO = QO.You need to find a point D on the circle, which makes PD+QD minimum.Output minimum distance sum.

Input

The first line of the input gives the number of test cases T; T test cases follow.Each case begins with one line with r : the radius of the circle C.Next two line each line contains two integers x , y denotes the coordinate of P and Q.LimitsT≤500000−100≤x,y≤1001≤r≤100

Output

For each case output one line denotes the answer.The answer will be checked correct if its absolute or relative error doesn't exceed 10−6.Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if |a−b|max(1,b)≤10−6.

Sample Input

444 00 440 33 040 22 040 11 0

Sample Output

5.65685435.65685435.89450306.7359174

Source

2017 Multi-University Training Contest - Team 6 

题意:

在圆上找一点 D, 算PD + QD 的最小值,

思路:

比赛时想到了在中垂线上的情况,如何写出来发现样例只能过后两个,于是就想是不是自己想错了,当时又想到高中时平面几何的类似方法,找出点关于圆的的边界的对称点,然后两点之间直线最短.然而由于太复杂,没有写出来,而且当时还没开始学计算几何,不知道反演(维基百科)是什么东西,待看到题解后发现是反演然后加上相似三角形就可以判了,

官方题解:

很不幸不总是中垂线上的点取到最小值,考虑点在圆上的极端情况。做P点关于圆的反演点P',OPD与ODP'相似,相似比是|OP| : r。Q点同理。极小化PD+QD可以转化为极小化P'D+Q'D。当P'Q'与圆有交点时,答案为两点距离,否则最优值在中垂线上取到。时间复杂度 O(1)O(1)O(1)也有代数做法,结论相同。优秀的黄金分割三分应该也是可以卡过的。

在图上做出反演点并连上三角形,我们得到 OP’ / OD = r / op,这是我们判断直线P’Q’与圆是否有交点,显然直线上离圆最近的点是线段P’Q’的中点:

当有交点是我们可以算到P’D + Q’D 的最小值,根据相似三角形 PD + QD = OPr (P’D+ Q’D);

如果没有交点 ,那么根据三角形DP’Q’三边的关系我们知道P’D + Q’D 不是最优解,而此时最优解就是中垂线与圆交点得到的D,

相似的性质巧妙运用可以大大简化计算.

代码:

#include<bits/stdc++.h>using namespace std;const double eps = 1e-6;double x1, y1, x2, y2, r;inline double dist(double x1, double y1,double  x2 , double y2){    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));}int main(){    int t;    scanf("%d", &t);    while(t--){        scanf("%lf %lf %lf %lf %lf", &r, &x1, &y1, &x2, &y2);        double op = dist(x1, y1, 0 , 0);        if(op <= eps){            printf("%.7f\n",r * 2);            continue;        }double rate = r * r / ( op * op);       double x3 = x1 * rate, y3 = y1 * rate;       double x4 = x2 * rate, y4 = y2 * rate;       double mx = (x3 + x4) / 2, my = (y3 + y4) / 2;       double dism = dist(mx, my, 0, 0);       if(dism <= r){            double ans = dist(x3, y3, x4, y4);            printf("%.7f\n", ans *(op / r));       } else {        double ratee = r / dism;        double mmx = mx * ratee;        double mmy = my * ratee;        double ans = dist(x1, y1, mmx, mmy);        printf("%.7f\n",ans * 2.0);       }    }return 0;}
原创粉丝点击