POJ--2420 A Star not a Tree?模拟退火求费马点

求一个点到给顶点距离和的最小值。

利用模拟退火。

#include <algorithm>    //STL通用算法
#include <bitset>     //STL位集容器
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>      //STL双端队列容器
#include <exception>    //异常处理类
#include <fstream>
#include <functional>   //STL定义运算函数(代替运算符)
#include <limits>
#include <list>      //STL线性列表容器
#include <map>       //STL 映射容器
#include <iomanip>
#include <ios>      //基本输入/输出支持
#include<iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>
#include <istream>     //基本输入流
#include <ostream>     //基本输出流
#include <queue>      //STL队列容器
#include <set>       //STL 集合容器
#include <sstream>    //基于字符串的流
#include <stack>      //STL堆栈容器    
#include <string>     //字符串类
#include <vector>     //STL动态数组容器
#define ll long long
using namespace std;
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 10000+66;
const int maxm=100000+66;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
const double INF=99999999;
const double esp=1e-9;
struct point
{
    double x;
    double y;
    double dis(point b)
    {
        return hypot(x-b.x,y-b.y);
    }
} p[maxn];
double dis(int n,point now)
{
    double ans=0;
    rep(i,1,n)
    {
        ans+=now.dis(p[i]);
    }
    return ans;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        rep(i,1,n)
        {
            scanf("%lf %lf",&p[i].x,&p[i].y);
        }
        double X=10000,Y=10000;
        double ans=0;
        point now;
        double x=0,y=0;
        now.x=0;
        now.y=0;
        ans=100000000;
        double step=100000,r=0.96;
        int cnt=0;
        while(step>esp)
        {
            double cx=x,cy=y,xans=ans;
            for(int i=0; i<8; i++)
            {
                now.x=x+step*dx[i];
                now.y=y+step*dy[i];
                if(now.x>=0&&now.y>=0&&now.x<X+esp&&now.y<Y+esp)
                {
                    double tmp=dis(n,now);
                    if(tmp<xans)
                    {
                        cx=now.x;
                        cy=now.y;
                        xans=tmp;
                    }
                }
            }
            x=cx;
            y=cy;
            ans=xans;
            step*=r;
        }
        printf ("%.0f\n", ans);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值