hdu 2440

              题目意思就是求费马点。

              凸包+随机贪心法可解。先构造一个凸包,因为凸包上的点才能保证监视范围够大,最后求凸包的费马点即可。

              以下是代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<utility>
#include<string>
#include<set>
#include<vector>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<map>
#include<cmath>
using namespace std;
#define FOR(i,a,b) for(int i(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define CL(a,b) memset(a,b,sizeof(a))

const int M=200; 
const int N=2000;
//const int inf=1<<23;
const double inf=1e250;
const double eps=1e-20;
const double pi=acos(-1.0);

struct point
{
    double x,y;
    bool operator < (const point a)const
    {
        if(y==a.y)
            return x<a.x;
        return y<a.y;
    }
}p[M],stac[M];
int n,top;

int xmult(double x1,double y1,double x2,double y2)
{
    return x1*y2-x2*y1;
}

int cross(point p0,point p1,point p2)
{
    double x1=p1.x-p0.x,y1=p1.y-p0.y;
    double x2=p2.x-p0.x,y2=p2.y-p0.y;
    return xmult(x1,y1,x2,y2);
}

void covex_hull()
{
    sort(p,p+n);
    stac[0]=p[0];
    stac[1]=p[1];
    top=1;
    for(int i=2;i<n;i++)
    {
        while(top>0 && cross(stac[top],p[i],stac[top-1])<=0)
            top--;
        stac[++top]=p[i];
    }
    int temp=top;
    for(int i=n-2;i>=0;i--)
    {
        while(top>temp && cross(stac[top],p[i],stac[top-1])<=0)
            top--;
        stac[++top]=p[i];
    }
    --top;
}

point getpoint(double x,double y)
{
    point temp;
    temp.x=x;temp.y=y;
    return temp;
}

double dist(point a,point b)
{
    return sqrt((b.x-a.x)*(b.x-a.x)*1.0+(b.y-a.y)*(b.y-a.y));
}

double alldis(point a)
{
    double dis=0;
    for(int i=0;i<=top;i++)
    {
        dis+=dist(a,stac[i]);
    }
    return dis;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        covex_hull();
        point pp=stac[0];
        double ans=inf,step=100;
        ans=alldis(pp);
        while(step>0.2)
        {
            bool flag=true;
            while(flag)
            {
                flag=false;
                point qq=getpoint(pp.x,pp.y+step),tt=pp;
                double temp=alldis(qq);
                if(temp<ans)
                {
                    ans=temp;
                    tt=qq;
                    flag=true;
                }

                qq=getpoint(pp.x,pp.y-step);
                temp=alldis(qq);
                if(temp<ans)
                {
                    ans=temp;
                    tt=qq;
                    flag=true;
                }

                qq=getpoint(pp.x+step,pp.y);
                temp=alldis(qq);
                if(temp<ans)
                {
                    ans=temp;
                    tt=qq;
                    flag=true;
                }

                qq=getpoint(pp.x-step,pp.y);
                temp=alldis(qq);
                if(temp<ans)
                {
                    ans=temp;
                    tt=qq;
                    flag=true;
                }

                pp=tt;
            }
            step=step/2.0;
        }
        int dis=(int)(ans+0.5)*100/100;
        printf("%d\n",dis);
        if(T>0) puts("");
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值