求一个点到给顶点距离和的最小值。
利用模拟退火。
#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);
}
}