Poj 3990 Fermat Point in Quadrangle convex hull and Fermat Point
Question:
Evaluate the Fermat point of a quadrilateral.
Analysis:
The simulated annealing either times out or wa, and the data in this question does not want to pass the random algorithm .. In fact, the fenma point of the quadrilateral is very simple. If it is a convex quadrilateral, The fenma point is the diagonal intersection point, and if it is a concave quadrilateral, The fenma point is a concave point. However, the order of the four vertices given by the question is uncertain, so we need to calculate the lower convex hull first.
Code:
//poj 3990//sep9#include
#include
#include using namespace std;struct Point{double x,y,v;Point(){}Point(double x,double y):x(x),y(y){}}pnt[8],rec[8];double getSum(Point p){double sum=0;for(int i=0;i<4;++i)sum+=sqrt((p.x-pnt[i].x)*(p.x-pnt[i].x)+(p.y-pnt[i].y)*(p.y-pnt[i].y));return sum;}int cmp(Point a,Point b){if(a.y!=b.y) return a.y
0?1:-1;}double dis(Point a,Point b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int cross(Point a,Point b,Point c){double x1=b.x-a.x;double y1=b.y-a.y;double x2=c.x-a.x;double y2=c.y-a.y;return dbl(x1*y2-x2*y1);}int graham(){int n=4;sort(pnt,pnt+n,cmp);rec[0]=pnt[0];rec[1]=pnt[1];int i,pos=1;for(i=2;i
0&&cross(rec[pos-1],rec[pos],pnt[i])<=0) --pos;rec[++pos]=pnt[i];}int bak=pos;for(i=n-1;i>=0;--i){while(pos>bak&&cross(rec[pos-1],rec[pos],pnt[i])<=0) --pos;rec[++pos]=pnt[i];}return pos;}int main(){int i;while(1){for(i=0;i<4;++i)scanf("%lf%lf",&pnt[i].x,&pnt[i].y);double ans=0;if(pnt[0].x<-0.5) break;if(graham()==4){ans+=dis(rec[0],rec[2]);ans+=dis(rec[1],rec[3]);}else{ans=1e10;for(int i=0;i<4;++i)ans=min(ans,getSum(pnt[i]));}printf("%.4lf\n",ans+1e-8);}return 0;}