栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3990 Fermat Point in Quad...

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3990 Fermat Point in Quad...

#include<iostream>#include<cstdio> #include<cmath> #include<algorithm> using namespace std; const double EP=1e-8; const double INF=1e8; struct Point{double x, y;}; Point p[4], pt[4], c; double x_mult(Point sp, Point ep, Point op){     return (sp.x-op.x)*(ep.y-op.y)-(sp.y-op.y)*(ep.x-op.x); } double dist(Point p1, Point p2){     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } bool cmp(const Point p1, const Point p2){     return p1.y<p2.y ||(p1.y==p2.y&&p1.x<p2.x); } int graham(Point pp[], Point res[]){     int i, len1, k=0, top=1;     sort(pp,pp+4,cmp);     res[0]=pp[0];     res[1]=pp[1];     res[2]=pp[2];     for(i=2; i<4; i++){         while(top&&x_mult(pp[i],res[top],res[top-1])> - EP)  top--;         res[++top]=pp[i];     }     len1=top; res[++top]=pp[2];     for(i=1; i>=0; i--){         while(top!=len1&&x_mult(pp[i],res[top],res[top-1])> - EP)  top--;  res[++top]=pp[i];     }     return top;       } Point intersect(Point u1,Point u2,Point v1,Point v2){       Point res=u1;       double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/     ((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));      res.x+=(u2.x-u1.x)*t;      res.y+=(u2.y-u1.y)*t;      return res; } int main(){     while(1){         double ans=0, s;         int i, flag, j;         for(i=0; i<4; i++)         scanf("%lf%lf", &p[i].x, &p[i].y);         if(p[0].x<0)break;         int sum=graham(p, pt);         if(sum==4){  c=intersect(pt[0], pt[2], pt[1], pt[3]);  for(i=0; i<4; i++)  ans+=dist(p[i], c);         }         else {  ans=INF;  for(i=0; i<4; i++){      s=0;      for(j=0; j<4; j++)      s+=dist(p[i], p[j]);      if(s<ans)          ans=s;  }         }         printf("%.4fn", ans);     }     return 0; }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377218.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号