Tranvick

simulated_annealing

Apr 6th, 2012
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:65777216")
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <cstdio>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <vector>
  10. #include <set>
  11. #include <stack>
  12. #include <map>
  13. #include <queue>
  14. #include <string>
  15. #include <memory.h>
  16. #include <iterator>
  17. #define y1 trololoy1
  18. #define y0 trololoy0
  19. #define mem(A,X) memset(A,X,sizeof(A))
  20. #define memo(A) memset(A,0,sizeof(A))
  21. #define forn(I,B) for (int I=1;I<=(B);I++)
  22. #define forg(H,V) for (int H=first[V];h;h=next[H])
  23. #define rep(I,B) for (int I=0;I<(B);I++)
  24. #define labs(X) (((X)>0)?(X):(-(X)))
  25. #define ropen(X) freopen(X,"r",stdin)
  26. #define wopen(X) freopen(X,"w",stdout)
  27. #define rwopen(X) freopen(X".in","r",stdin);freopen(X".out","w",stdout)
  28. #define pb push_back
  29. #define mp make_pair
  30. #define all(X) (X).begin(),(X).end()
  31. #define sqr(X) ((X)*(X))
  32.  
  33. using namespace std;
  34.  
  35. typedef pair <int,int> pii;
  36. typedef double ld;
  37. typedef long long ll;
  38. typedef pair <ll,ll> pll;
  39. typedef vector<int> vi;
  40. const int N=222222;
  41. const int INF=111111111;
  42. const double eps=1e-9;
  43. const double pi=3.14159265358979;
  44.  
  45. int n,a[N],ptr,g[N];
  46. bool b[N];
  47. ld ans=0,x[N],y[N],t=1000.0,minans,t0=1000.0;
  48.  
  49. inline ld dist(int i,int j){
  50.     return (sqrt(sqr(x[i]-x[j])+sqr(y[i]-y[j])));
  51. }
  52.  
  53. void solve_greedy(){
  54.     int cur=0;
  55.     b[0]=1;a[ptr++]=0;
  56.     rep(steps,n-1){
  57.         int num=-1;
  58.         rep(i,n) if (!b[i] && (num==-1 || dist(cur,num)>dist(cur,i))) num=i;
  59.         ans+=dist(cur,num);
  60.         cur=num;
  61.         a[ptr++]=cur;
  62.         b[cur]=a;
  63.     }
  64.     ans+=dist(a[ptr-1],0);
  65.     minans=ans;
  66. }
  67.  
  68. inline ld get_ans(){
  69.     ld cur=0;
  70.     g[n]=0;
  71.     rep(i,n) cur+=dist(g[i],g[i+1]);
  72.     return cur;
  73. }
  74.  
  75. inline void get_next(){
  76.     int k=n*(t/t0);
  77.     if (k<2) k=2;
  78.     rep(i,n) g[i]=a[i];
  79.     while(k--){
  80.         int num1=rand()%(n-1);
  81.         while (g[num1]==0) num1=rand()%(n-1);
  82.         swap(g[num1],g[num1+1]);
  83.     }
  84. }
  85.  
  86. inline ld get_rand(){
  87.     return ((rand()*1ll*rand())%1000001)/1000000.0;
  88. }
  89.  
  90. int main(){
  91.     ropen("input.txt");
  92.     wopen("output.txt");
  93.     srand(time(NULL));
  94.     cin>>n;
  95.     rep(i,n) cin>>x[i]>>y[i];
  96.     solve_greedy();
  97.     int k=20000;
  98.     while (k--){
  99.         get_next();
  100.         ld ff=get_ans();
  101.         if (ff<ans){
  102.             ans=ff;
  103.             rep(i,n) a[i]=g[i];
  104.         } else {
  105.             ld delta=ff-ans;
  106.             ld p=exp(-delta/t);
  107.             if (get_rand()<=p){
  108.                 ans=ff;
  109.                 rep(i,n) a[i]=g[i];
  110.             }
  111.         }
  112.         t*=0.99;
  113.         minans=min(minans,ans);
  114.     }
  115.     printf("%lf\n",minans);
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment