Poj 3990 Fermat Point in Quadrangle convex hull and Fermat Point

Source: Internet
Author: User
Tags cmath

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;} 
    
   
  
 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.