Test instructions
Ask for a four-sided Fermat point.
Analysis:
Simulated annealing either timed out or WA, the data of this problem is not want to let random algorithm over. In fact, the four-sided Fermat point is very simple, if it is convex four-sided call horse point is the diagonal intersection, if the concave quadrilateral cost horse point is a concave point. However, the four point order given by the topic is indeterminate, so the convex hull must be obtained first.
Code:
POJ 3990//sep9#include <iostream> #include <cmath> #include <algorithm>using namespace std;struct Point{double x,y,v; Point () {}point (double x,double y): x (x), Y (y) {}}pnt[8],rec[8];d ouble 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<b.y;return a.x<b.x;} int dbl (double x) {if (Fabs (x) <1e-7) return 0;return x>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<n;++i) {while (pos >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);d ouble 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;}
POJ 3990 Fermat Point in quadrangle convex and pony points