HDU 3694 Fermat Point in Quadrangle (数学-费马点)

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


题意:给你四个点,让你求他们的费马点(求一个点到这四个点的距离只和最小)。

题解:通过费马点定理,我们可以得出,当这四个点是凸多边形那么费马点就是对角线的交点,当时凹多边形的时候,费马点为凹进去的那个点。


AC代码:

Accepted 3694 0MS 344K 2985 B G++ XH_Reventon

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <cfloat>
using namespace std;

#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s)  scanf("%s",s)
#define pi1(a)    printf("%d\n",a)
#define pi2(a,b)  printf("%d %d\n",a,b)
#define mset(a,b)   memset(a,b,sizeof(a))
#define forb(i,a,b)   for(int i=a;i<b;i++)
#define ford(i,a,b)   for(int i=a;i<=b;i++)

typedef long long LL;
const int N=6;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-8;

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

struct Line
{
    double a,b,c;
};

bool mult(point sp, point ep, point op)
{
	return (sp.x - op.x) * (ep.y - op.y) >= (ep.x - op.x) * (sp.y - op.y);
}

bool operator < (const point &l, const point &r)
{
    return l.y < r.y || (l.y == r.y && l.x < r.x);
}

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 tubao(point pnt[], int n)
{
	int i, len, top = 1;
	sort(pnt, pnt + n);
	if (n == 0)		return 0;
    res[0] = pnt[0];
	if (n == 1)		return 1;
    res[1] = pnt[1];
	if (n == 2)		return 2;
    res[2] = pnt[2];
	for (i = 2; i < n; i++)
	{
		while (top && mult(pnt[i], res[top], res[top-1]))
			top--;
		res[++top] = pnt[i];
	}
	len = top; res[++top] = pnt[n - 2];
	for (i = n - 3; i >= 0; i--)
	{
		while (top!=len && mult(pnt[i], res[top], res[top-1])) top--;
			res[++top] = pnt[i];
	}
	return top; // 返回凸包上顶点的个数,不包括在凸包边上的点
}

Line get_Line(point p1,point p2)//两点求直线
{
    Line tmp;
    tmp.a=p1.y-p2.y;
    tmp.b=p2.x-p1.x;
    tmp.c=p1.x*p2.y-p2.x*p1.y;
    return tmp;
}

point Line_Line(Line m1, Line m2)//两条直线交点
{
    point tmp;
    tmp.x=(m1.b*m2.c-m2.b*m1.c)/(m1.a*m2.b-m2.a*m1.b);
    tmp.y=(m1.c*m2.a-m2.c*m1.a)/(m1.a*m2.b-m2.a*m1.b);
    return tmp;
}

double xiaohao(point t)
{
    double sum=0;
    forb(i,0,4)
        sum+=dis(t,p[i]);
    return sum;
}

int main()
{
    while(scanf("%lf%lf",&p[0].x,&p[0].y))
    {
        if(p[0].x<0)    break;
        for(int i=1;i<4;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        int n=tubao(p,4);
        if(n==4)//凸多边形
        {
            Line l1=get_Line(res[0],res[2]),l2=get_Line(res[1],res[3]);
            point t=Line_Line(l1,l2);
            double sum=xiaohao(t);
            printf("%.4f\n",sum);
        }
        else//凹多边形
        {
            double mi=xiaohao(p[0]);
            for(int i=1;i<4;i++)
                mi=min(mi,xiaohao(p[i]));
            printf("%.4f\n",mi);
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/ilovexiaohao/article/details/12231849