HDU 3694Fermat Point in Quadrangle(三分 不过网上大多是费马点的题解)

来源:互联网 发布:国外期刊 数据库 编辑:程序博客网 时间:2024/06/13 09:36

Fermat Point in Quadrangle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1545    Accepted Submission(s): 256


Problem Description
In geometry the Fermat point of a triangle, also called Torricelli point, is a point such that the total distance from the three vertices of the triangle to the point is the minimum. It is so named because this problem is first raised by Fermat in a private letter. In the following picture, P0 is the Fermat point. You may have already known the property that:

Alice and Bob are learning geometry. Recently they are studying about the Fermat Point. 

Alice: I wonder whether there is a similar point for quadrangle.

Bob: I think there must exist one.

Alice: Then how to know where it is? How to prove?

Bob: I don’t know. Wait… the point may hold the similar property as the case in triangle. 

Alice: It sounds reasonable. Why not use our computer to solve the problem? Find the Fermat point, and then verify your assumption.

Bob: A good idea.

So they ask you, the best programmer, to solve it. Find the Fermat point for a quadrangle, i.e. find a point such that the total distance from the four vertices of the quadrangle to that point is the minimum.
 

Input
The input contains no more than 1000 test cases.

Each test case is a single line which contains eight float numbers, and it is formatted as below:

x1 y1 x2 y2 x3 y3 x4 y4

xi, yi are the x- and y-coordinates of the ith vertices of a quadrangle. They are float numbers and satisfy 0 ≤ xi ≤ 1000 and 0 ≤ yi ≤ 1000 (i = 1, …, 4).

The input is ended by eight -1.
 

Output
For each test case, find the Fermat point, and output the total distance from the four vertices to that point. The result should be rounded to four digits after the decimal point.
 

Sample Input
0 0 1 1 1 0 0 11 1 1 1 1 1 1 1-1 -1 -1 -1 -1 -1 -1 -1
 

Sample Output
2.82840.0000
 

Source
2010 Asia Fuzhou Regional Contest
 

题目大意:题目意思很简单,给你四个点,问你求一个点使得到四个顶点的距离最小。网上大多是费马点的解释方法,终于知道便利店选址还有一个数学方法就是费马点。。。开始看了时间,才给了1s,觉得应该会WA,或者会TLE。真没想到会A,不过计算几何费马点也得学习下,速度都不是一个档次的。


AC代码:
#include<iostream>#include<cstring>#include<string>#include<cmath>#include<cstdio>using namespace std;double eps=1e-7;double minx,maxx,miny,maxy;  //记录边界大小,三分struct mq{    double x;    double y;};mq node[4];double dis(double xx,double yy){    int i;    double sum=0;    for(i=0;i<4;i++)        sum+=sqrt((xx-node[i].x)*(xx-node[i].x)+(yy-node[i].y)*(yy-node[i].y));    return sum;}double cal(double x)  //x确定后,寻找y最小{    double ly,ry,midy,mimidy;    ly=miny,ry=maxy;    midy=(ly+ry)/2.0;    while(ry-ly>eps)    {        midy=(ly+ry)/2.0,mimidy=(midy+ry)/2.0;        if(dis(x,midy)<dis(x,mimidy))            ry=mimidy;        else            ly=midy;    }    return dis(x,midy);}int main(){    int i;    while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&node[0].x,&node[0].y,&node[1].x,&node[1].y,&node[2].x,&node[2].y,&node[3].x,&node[3].y))    {        if(node[0].x<0) break;        minx=maxx=node[0].x,miny=maxy=node[0].y;        for(i=1;i<4;i++)        {            if(node[i].x<minx) minx=node[i].x;            if(node[i].x>maxx) maxx=node[i].x;            if(node[i].y<miny) miny=node[i].y;            if(node[i].y>maxy) maxy=node[i].y;        }        double lx,rx,midx,mimidx;        lx=minx,rx=maxx;        midx=(lx+rx)/2.0;        while(rx-lx>eps)        {            midx=(lx+rx)/2.0,mimidx=(midx+rx)/2.0;            if(cal(midx)<cal(mimidx))                rx=mimidx;            else                lx=midx;        }        double res=cal(midx);        printf("%.4f\n",res);    }    return 0;}//515MS