POJ 2420 A Star not a Tree? (爬山法||模拟退火)
2015-11-24 11:28
399 查看
爬山法+模拟退火:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
#include<math.h>
#include<ctime>
#include<cstdlib>
using namespace std ;
struct node
{
double x,y;
}q[120];
double xx,yy;
int n;
double dis(double x,double y,node p)
{
return sqrt( (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y) );
}
double getsum(double a,double b)
{
double tmp=0;
for(int i=0;i<n;i++)
tmp+=dis(a,b,q[i]);
return tmp;
}
int main()
{
int m,i,j,k;
while(~scanf("%d",&n))
{
double ans;
xx=0,yy=0;double t=10000;
for(i=0;i<n;i++)
{
scanf("%lf%lf",&q[i].x,&q[i].y);
xx+=q[i].x;yy+=q[i].y;
}
xx/=n;yy/=n;
ans=getsum(xx,yy);
double tmp,x,y;
while(t>0.02)
{
x=y=0;
for(i=0;i<n;i++)
{
x+=(q[i].x-xx)/dis(xx,yy,q[i]);
y+=(q[i].y-yy)/dis(xx,yy,q[i]);
}
tmp=getsum(xx+x*t,yy+y*t);
if(tmp<ans)
{
ans=tmp;
xx+=x*t;yy+=y*t;
}
/*else if(log( (tmp-ans)/t)<(rand()%10000/10000.0) )//注意使用的cstdlib/ctime头文件
{
ans=tmp;
xx+=x*t;yy+=y*t;
}*////模拟退火和爬山就是一个else if(log(tmp-ans )/t<(rand()%10000)/10000.0 )的区别
t*=0.9;
}
printf("%.0lf\n",ans);
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
#include<math.h>
#include<ctime>
#include<cstdlib>
using namespace std ;
struct node
{
double x,y;
}q[120];
double xx,yy;
int n;
double dis(double x,double y,node p)
{
return sqrt( (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y) );
}
double getsum(double a,double b)
{
double tmp=0;
for(int i=0;i<n;i++)
tmp+=dis(a,b,q[i]);
return tmp;
}
int main()
{
int m,i,j,k;
while(~scanf("%d",&n))
{
double ans;
xx=0,yy=0;double t=10000;
for(i=0;i<n;i++)
{
scanf("%lf%lf",&q[i].x,&q[i].y);
xx+=q[i].x;yy+=q[i].y;
}
xx/=n;yy/=n;
ans=getsum(xx,yy);
double tmp,x,y;
while(t>0.02)
{
x=y=0;
for(i=0;i<n;i++)
{
x+=(q[i].x-xx)/dis(xx,yy,q[i]);
y+=(q[i].y-yy)/dis(xx,yy,q[i]);
}
tmp=getsum(xx+x*t,yy+y*t);
if(tmp<ans)
{
ans=tmp;
xx+=x*t;yy+=y*t;
}
/*else if(log( (tmp-ans)/t)<(rand()%10000/10000.0) )//注意使用的cstdlib/ctime头文件
{
ans=tmp;
xx+=x*t;yy+=y*t;
}*////模拟退火和爬山就是一个else if(log(tmp-ans )/t<(rand()%10000)/10000.0 )的区别
t*=0.9;
}
printf("%.0lf\n",ans);
}
return 0;
}
相关文章推荐
- 转-局部搜索的形象描述
- 模拟退火求解TSP问题<2变换法产生路径>
- 模拟退火算法的详细理解
- 基于模拟退火算法求解TSP问题(JAVA)
- 【HNOI2011】【BZOJ2336】任务调度
- POJ 2420 A Star not a Tree? (模拟退火)
- hdu 5017 Ellipsoid(模拟退火)
- ZOJ 2928 模拟退火 求三维费马点
- POJ 2069 Super Star(模拟退火,最小球覆盖)
- 利用模拟退火算法求解TSP问题(C++实现)
- 模拟退火合集
- POJ 2420 A Star not a Tree?(计算几何 多边形的费马点 模拟退火)
- 模拟退火算法原理及求解TSP问题的Java实现
- [bzoj3680] 吊打XXX:模拟退火 or 模拟力学情景
- [BZOJ2428][HAOI2006]均分数据(模拟退火)
- [BZOJ2832][宅男小C][模拟退火]
- JSOI2016 炸弹攻击Attack
- bzoj 2428: [HAOI2006]均分数据 (模拟退火)
- BZOJ 1379 模拟退火
- BZOJ 3680 模拟退火