/*
参考:http://blog.sina.com.cn/s/blog_64675f540100sehz.html
题意:给定n个点,找到一个点,使得n个点到这个点的和值最小
模拟退火法
模拟退火的过程
1 找到这些点所在的范围,用两个点框定(代码中e1,e2两个点)
2 在这个范围内生成NUM个点(NUM自定)
3 对于每个生成的点i,在其周围生成NUM个点,一旦有点优于i,则替换。
4 缩小范围D,若D<精度,退出,否则执行 3
5 遍历所有NUM个点,找到val的最大值
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include<map>
#include<ctime>
using namespace std;
const int NUM=30;
const int RAD=1000;
struct point
{
double x,y,val;
point(){}
point(double _x,double _y):x(_x),y(_y){}
}p[101],May[NUM],e1,e2;
int n;
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double judge(point t)//评价函数,得到点t的评价值val
{
double len=0;
for(int i=0;i<n;i++)
len+=dis(t,p[i]);
return len;
}
double Rand(){return rand()%(RAD+1)/(1.0*RAD);}//随机产生0-1的浮点数
point Rand_point(point a,point b)//在a,b框定的四边形内随机生成点
{
point tmp=point(a.x+(b.x-a.x)*Rand(),a.y+(b.y-a.y)*Rand());
tmp.val=judge(tmp);
return tmp;
}
void solve(double D)
{
for(int i=0;i<NUM;i++)
May[i]=Rand_point(e1,e2);//步骤2
while(D>0.1)//步骤 3
{
for(int i=0;i<NUM;i++)
for(int j=0;j<NUM;j++)
{
point tmp=Rand_point(point(May[i].x-D,May[i].y-D),point(May[i].x+D,May[i].y+D));
if(tmp.val<May[i].val)
{
May[i]=tmp;
}
}
D*=0.9;
}
double ans=1LL<<45;
for(int i=0;i<NUM;i++)
if(May[i].val<ans)
ans=May[i].val;
printf("%0.f\n",ans);
}
int main()
{
srand(time(0));
e1=point(-10001,-10001);
e2=point(10001,10001);
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
e1.x=min(e1.x,p[i].x);//框定初始范围
e1.y=min(e1.y,p[i].y);
e2.x=max(e2.x,p[i].x);
e2.y=max(e2.y,p[i].y);
}
solve(max(e2.y-e1.y,e2.x-e1.x));
}
}
POJ 2420 A Star not a Tree? 费马点,模拟退火
最新推荐文章于 2019-09-25 13:53:46 发布