码迷,mamicode.com
首页 > 编程语言 > 详细

模拟退火算法

时间:2015-04-09 21:34:45      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

这是我见过的最神的乱搞!

先开坑.

 

 

 

 

AC POJ 2420

技术分享
  1 #include <cstdio>
  2 #include <fstream>
  3 #include <iostream>
  4  
  5 #include <cstdlib>
  6 #include <cstring>
  7 #include <algorithm>
  8 #include <cmath>
  9  
 10 #include <queue>
 11 #include <vector>
 12 #include <map>
 13 #include <set>
 14 #include <stack>
 15 #include <list>
 16  
 17 typedef unsigned int uint;
 18 typedef long long int ll;
 19 typedef unsigned long long int ull;
 20 typedef double db;
 21  
 22 using namespace std;
 23  
 24 inline int getint()
 25 {
 26     int res=0;
 27     char c=getchar();
 28     bool mi=false;
 29     while(c<0 || c>9) mi=(c==-),c=getchar();
 30     while(0<=c && c<=9) res=res*10+c-0,c=getchar();
 31     return mi ? -res : res;
 32 }
 33 inline ll getll()
 34 {
 35     ll res=0;
 36     char c=getchar();
 37     bool mi=false;
 38     while(c<0 || c>9) mi=(c==-),c=getchar();
 39     while(0<=c && c<=9) res=res*10+c-0,c=getchar();
 40     return mi ? -res : res;
 41 }
 42  
 43 db eps=1e-20;
 44 inline bool feq(db a,db b)
 45 { return fabs(a-b)<eps; }
 46 
 47 template<typename Type>
 48 inline Type avg(const Type a,const Type b)
 49 { return a+((b-a)/2); }
 50 
 51 
 52 //==============================================================================
 53 //==============================================================================
 54 //==============================================================================
 55 //==============================================================================
 56 
 57 
 58 
 59 int n;
 60 
 61 db x[105];
 62 db y[105];
 63 
 64 inline db dis2(db x1,db y1,db x2,db y2)
 65 { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); }
 66 
 67 inline db getdist(db X,db Y)
 68 {
 69     db l=0;
 70     for(int i=0;i<n;i++)
 71     l+=dis2(X,Y,x[i],y[i]);
 72     return l;
 73 }
 74 
 75 int _cnt=0;
 76 int rands=0;
 77 inline int getrand()
 78 {
 79     _cnt++;
 80     if(_cnt==32768*32768) srand(++rands);
 81     return rand()+32768*rand();
 82 }
 83 
 84 inline db dbrand()
 85 { return (db)(rand()+32768*rand())/(db)(32768*32768); }
 86 
 87 db res;
 88 
 89 
 90 int main()
 91 {
 92     srand(rands);
 93     
 94     while(scanf("%d",&n)>0)
 95     {
 96         for(int i=0;i<n;i++)
 97         {
 98             x[i]=getint();
 99             y[i]=getint();
100         }
101         
102         //Searching.
103         db cx=0,cy=0;
104         res=getdist(cx,cy);
105         
106         db T=1e5;
107         
108         while(T>=1e-2)
109         {
110             db rx=cx+(dbrand()-0.5)*T*2.0;
111             db ry=cy+(dbrand()-0.5)*T*2.0;
112             db dist=getdist(rx,ry);
113             db d=res-dist;
114             if(d>0)
115             {
116                 res=dist;
117                 cx=rx;
118                 cy=ry;
119             }
120             T*=0.98;
121         }
122         
123         printf("%.0f\n",res);
124     }
125     
126     
127     return 0;
128 }
View Code

注意程序写的并不是完整的模拟退火....我们只接受最优参数,而不是接受一些比较差的参数......

不过这里的搜索范围使用了温度计数T....

db rx=cx+(dbrand()-0.5)*T*2.0;
db ry=cy+(dbrand()-0.5)*T*2.0;

这应该也算模拟退火吧(模拟分子的不规则运动)........反正能A掉这个题........

 

 

 

 

 


具体算法参考

http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html

 

模拟退火算法

标签:

原文地址:http://www.cnblogs.com/DragoonKiller/p/4411833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!