HDU3694Fermat Point in Quadrangle

http://acm.hdu.edu.cn/showproblem.php?pid=3694


10fuzhou

求四边形中某点使之到其它四点距离之和最小。


若为凸多边形则是对角线交点,否则是凹入的点,但此点不好求,不过枚举每个顶点即可。

注意printf("%f",double);会WA。


bool ral(point p1, point p2, point p3)//用叉乘判断点的位置
{
    return (p2.x - p1.x)*(p3.y - p1.y) > (p3.x - p1.x)*(p2.y - p1.y);
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string>
using namespace std;
const double eps=1e-7;
//const double INF=1e50;
//const double pi=acos(-1);

#define N 1005
#define M 20005

struct point
{
    double x,y;
}p[10];

double ldis(int a,int b)
{
    return (p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y);
}

double vcom(int a,int b,int c,int d)
{
    return (p[b].x-p[a].x)*(p[d].x-p[c].x)+(p[b].y-p[a].y)*(p[d].y-p[c].y);
}

double dis(int a,int b,int c)
{
    double l1,l2,v1;
    l1=ldis(a,b);
    l2=ldis(b,c);

    v1=vcom(b,a,b,c);
    v1*=2;

    return l1+l2-v1;
}

bool ral(point p1, point p2, point p3)//用叉乘判断点的位置
{
    return (p2.x - p1.x)*(p3.y - p1.y) > (p3.x - p1.x)*(p2.y - p1.y);
}

void swapp(int a,int b)
{
    double x1,y1;
    x1=p[a].x;
    p[a].x=p[b].x;
    p[b].x=x1;
    y1=p[a].y;
    p[a].y=p[b].y;
    p[b].y=y1;
}

void psort()
{
    if (ral(p[1],p[2],p[3])==false) swapp(2,3);
    if (ral(p[1],p[2],p[4])==false) swapp(2,4);

    if (ral(p[2],p[3],p[4])==false) swapp(3,4);
}

int main()
{
    //freopen("a","r",stdin);

    while (1)
    {
        for (int i=1;i<=4;i++)
        scanf("%lf%lf",&p[i].x,&p[i].y);
        if (p[1].x<0) break;

        psort();
        /*cout<<p[1].x<<' '<<p[1].y<<endl;
         cout<<p[2].x<<' '<<p[2].y<<endl;
          cout<<p[3].x<<' '<<p[3].y<<endl;
           cout<<p[4].x<<' '<<p[4].y<<endl;*/

        double d1,d2;
        d1=dis(1,2,3);
        d2=dis(2,3,4);
        double dis1;

        if ( (ral(p[4],p[1],p[2])&&ral(p[1],p[2],p[3])&&ral(p[2],p[3],p[4])&&ral(p[3],p[4],p[1])) ==false )//四个都要判断
        {
double dis2; dis1=sqrt(ldis(1,2))+sqrt(ldis(1,3))+sqrt(ldis(1,4)); dis2=sqrt(ldis(2,1))+sqrt(ldis(2,3))+sqrt(ldis(2,4)); if (dis2<dis1) dis1=dis2; dis2=sqrt(ldis(3,2))+sqrt(ldis(3,1))+sqrt(ldis(3,4)); if (dis2<dis1) dis1=dis2; dis2=sqrt(ldis(4,2))+sqrt(ldis(4,1))+sqrt(ldis(4,3)); if (dis2<dis1) dis1=dis2; } else dis1=sqrt(d1)+sqrt(d2); printf("%.4f\n",dis1); } return 0;}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值