您的位置:首页 > 其它

poj 2420 A Star not a Tree?

2016-04-25 10:11 429 查看
萌萌哒传送门

基本上代码和黄学长的一毛一样

模拟退火

主要是生成新状态和评价函数搞不清怎么弄。。

自己YY的一个每次跑出来答案不一样T_T

有人说是以要移动的点为中心,以2t为长的正方形中的随机一个移动位置来求最优值。。

看了代码发现他那个讲道理也不算模拟退火,因为没有概率接受非更优解。。

然而为甚么黄学长在生成新状态没有用随机化。。。

最后得出结论:大神思路太diao吾等蒟蒻只配%%%

代码(有谁看懂了给解释下好么。。):

#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define g getchar()
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
inline ll read(){
ll x=0,f=1;char ch=g;
for(;ch<'0'||ch>'9';ch=g)if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=g)x=x*10+ch-'0';
return x*f;
}
inline void out(ll x){
int a[25],wei=0;
if(x<0)putchar('-'),x=-x;
for(;x;x/=10)a[++wei]=x%10;
if(wei==0){puts("0");return;}
for(int j=wei;j>=1;--j)putchar('0'+a[j]);
putchar('\n');
}
struct re{
int x,y;
}p[105];
int n;
inline double sqr(double x){return x*x;}
double dis(double x,double y,re p)
{return sqrt(sqr(x-p.x)+sqr(y-p.y));}
double getsum(double x,double y){
double tmp=0;
for(int i=1;i<=n;++i)tmp+=dis(x,y,p[i]);
return tmp;
}
int main(){
srand(time(0));
while(scanf("%d",&n)==1){
double xx=0,yy=0;double ans=1e20;double t=100000;
for(int i=1;i<=n;++i){
p[i].x=read();p[i].y=read();
xx+=p[i].x;yy+=p[i].y;
}
xx/=n;yy/=n;
ans=getsum(xx,yy);
double tmp,x,y;
for(;t>0.02;t*=0.9){
x=y=0;
for(int i=1;i<=n;++i){                       //生成新状态
x+=(p[i].x-xx)/dis(xx,yy,p[i]);
y+=(p[i].y-yy)/dis(xx,yy,p[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){          //随机化判定是否接受移动
ans=tmp;xx+=x*t;yy+=y*t;
}
}
printf("%.0lf",ans);
}
}


萌萌哒传送门2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: