hdu3694 Fermat Point in Quadrangle 求四边形费马点

http://acm.hdu.edu.cn/showproblem.php?pid=3694

Fermat Point in Quadrangle

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


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, P 0 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:

x 1 y 1 x 2 y 2 x 3 y 3 x 4 y 4

x i, y i are the x- and y-coordinates of the ith vertices of a quadrangle. They are float numbers and satisfy 0 ≤ x i ≤ 1000 and 0 ≤ y i ≤ 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 1 1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1
 

Sample Output
  
  
2.8284 0.0000
 

Source


题意:给个四边形我,问一个点到四边形四个点距离最小的距离和是多少。

分析:如果是凸四边形,费马点就是对角线的交点,距离就是对角线长度。这个很好证明。。

如果是凹多边形,费马点就是那个凹点,看图:


首先CD上的任意点距离肯定大于D点的。然后对于任意点E,它的距离和大于F的。

一个是FA+FD+FC+FB 一个是EA+EB+ED+EC 由三角形两边和大于第三边EA+EB>FA+FB EA=EF+FA EF+EB>FB得证。。

具体实现的时候弱求了个凸包,由于有重点等的情况当凸包点数<=3时枚举的点求最小距离。

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-8;
const double pi=acos(-1.0);
const int INF=0x7fffffff;
const LL inf=(((LL)1)<<61)+5;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    if(x>0) return 1;
    return -1;
}
struct point{
    double x,y;
    point(double x=0,double y=0):x(x),y(y) {}
};
point operator +(const point &a,const point &b){
	return point(a.x+b.x,a.y+b.y);
}
point operator -(const point &a,const point &b){
	return point(a.x-b.x,a.y-b.y);
}
point operator *(const point &a,const double &p){
	return point(a.x*p,a.y*p);
}
point operator /(const point &a,const double &p){
	return point(a.x/p,a.y/p);
}
bool operator < (const point &a,const point &b){
	return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
double dot(point A,point B){
    return A.x*B.x+A.y*B.y;
}
double cross(point A,point B){
    return A.x*B.y-A.y*B.x;
}
double Length(point A){
    return sqrt(dot(A,A));
}
int graham(point *p,int n,point *ch) //凸包
{
    sort(p,p+n);
    int m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
        ch[m++]=p[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
        ch[m++]=p[i];
    }
    if(n>1) m--;
    return m;
}
int main()
{
    point p[5],ch[5];
    while(true)
    {
        for(int i=0;i<4;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        if(p[0].x==-1) break;
        int m=graham(p,4,ch);
        double ans=1.0*INF;
        if(m<=3)
        {
            for(int i=0;i<4;i++)
            {
                double sum=0;
                for(int j=0;j<4;j++)
                {
                    if(j==i) continue;
                    sum+=Length(p[i]-p[j]);
                }
                if(sum<ans) ans=sum;
            }
        }
        else ans=Length(ch[0]-ch[2])+Length(ch[1]-ch[3]);
        printf("%.4lf\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值