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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章