poj 2420 A Star not a Tree?
来源:互联网 发布:shell编程 ${ 编辑:程序博客网 时间:2024/06/03 14:49
萌萌哒传送门
基本上代码和黄学长的一毛一样
模拟退火
主要是生成新状态和评价函数搞不清怎么弄。。
自己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 0x3f3f3f3fusing 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
0 0
- POJ 2420 A Star not a Tree?
- poj 2420 A Star not a Tree?
- poj 2420 A Star not a Tree?
- POJ 2420 A Star not a Tree?
- 【POJ 2420】A Star not a Tree?
- POJ 2420 A Star not a Tree?
- POJ 2420 A Star not a Tree?
- poj 2420 A Star not a Tree?
- poj 2420 A Star not a Tree?
- poj 2420 A Star not a Tree?
- poj 2420 A Star not a Tree?
- POJ 2420:A Star not a Tree?
- POJ 2420 A Star not a Tree?
- 【POJ 2420】A Star not a Tree?
- POJ 2420 A Star not a Tree? 费马点,模拟退火
- poj 2420 A Star not a Tree? 模拟退火
- POJ 2420 A Star not a Tree?(二维费马点)
- poj 2420 A Star not a Tree?(模拟退火求费马点)
- CAT ----分布式实时监控系统
- (转载)数据库范式及宽表窄表理解
- .DRPM File Extension
- 【数据结构】二叉树的原理及实现学习总结
- 网络爬虫实践(二)-动态页面
- poj 2420 A Star not a Tree?
- C语言复习
- 写自己的ASP.NET MVC框架(上)
- 销售易
- SQLZOO(SELECT from WORLD Tutorial)Writeup
- 随机数的实现原理
- finally关键字的理解
- 系统重力感应开关和Activity的屏幕方向属性值之间的区别
- Android调用Webservice