Accept: 96 Submit: 539
Time Limit: 1000 mSec Memory Limit : 32768 KB
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:
![](http://acm.fzu.edu.cn/image/Problem/1060005_1.jpg)
![](http://acm.fzu.edu.cn/image/Problem/1060005_2.jpg)
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
Sample Input
Sample Output
Source
The 35th ACM/ICPC Asia Regional Fuzhou Site
思路:本题要求四边形的费马点,四边形分为凹,凸,退化三种四边形(退化即为有点重合了);
显然凸四边形的费马点为对角线的交点,凹四边形的费马点为凹进去的点,退化四边形的费马点显然为退化的那个点;
综上,可以将其分为两类费马点要么在顶点上,要么在凸四边形的对角线上的交点上;
ps:判断是凸四边形可以参考线段相交点击打开链接;
代码:
#include <bits/stdc++.h>
using namespace std;
const int eps=1e-8;
double x[5],y[5];
double Min=100000.0;
double det(int a,int b,int c)//向量外积
{
return (x[b]-x[a])*(y[c]-y[a])-(x[c]-x[a])*(y[b]-y[a]);
}
double getdis(double x1,double y1,double x2,double y2)//两点距离
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void fmd(double fx,double fy)
{
double sum=0.;
for(int i=1;i<=4;i++) sum+=getdis(fx,fy,x[i],y[i]);
Min=min(sum,Min);
}
int comp(double d)
{
if(fabs(d)<eps)//避免误差
return 0;
return (d>0)?1:-1;
}
int intersect(int a,int b,int c,int d)//判断是否为凸四边形
{
int d1 = comp(det(a,c,d));
int d2 = comp(det(b,c,d));
int d3 = comp(det(c,a,b));
int d4 = comp(det(d,a,b));
if(d1*d2<0 && d3*d4<0)
return true;
return false;
}
int main()
{
while(scanf("%lf",&x[1])&&x[1]>=0)
{
scanf("%lf",&y[1]);
for(int i=2;i<=4;i++) scanf("%lf%lf",&x[i],&y[i]);
for(int i=1;i<=4;i++) fmd(x[i],y[i]);//判断4个顶点
if(intersect(1,2,3,4))
Min=getdis(x[1],y[1],x[2],y[2])+getdis(x[3],y[3],x[4],y[4]);
if(intersect(1,3,2,4))
Min=getdis(x[1],y[1],x[3],y[3])+getdis(x[2],y[2],x[4],y[4]);
if(intersect(1,4,2,3))
Min=getdis(x[1],y[1],x[4],y[4])+getdis(x[2],y[2],x[3],y[3]);
printf("%.4lf\n",Min);
}
return 0;
}