Advertisement
Alex239

Untitled

Apr 22nd, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. vector<Line> FSPGraph(vector<Line> x)
  2. {
  3.     double t = 10000;
  4.     long curclc = calc(x);
  5.     long maxclc = curclc;
  6.     int maxans = 0;
  7.     vector<pair<int, int>> hist;
  8.     int i, cnt = 0;
  9.     for (i = 0; t > 0.00000001; ++i)
  10.     {
  11.         t *= 0.99999;
  12.         bool type = rand() & 1;
  13.         long clcnxt = 0;
  14.         int s1 = -1, s2 = -1;
  15.         if (type)
  16.         {
  17.             s1 = rand() % x.size();
  18.             s2 = rand() % x.size();
  19.             int s11 = s1 - 1;
  20.             int s12 = s1 + 1;
  21.             int s21 = s2 - 1;
  22.             int s22 = s2 + 1;
  23.             clcnxt = curclc;
  24.             if (s11 >= 0)
  25.             {
  26.                 clcnxt -= dist(x[s11], x[s1]);
  27.                 clcnxt += dist(x[s11], x[s2]);
  28.             }
  29.             if (s12 < x.size())
  30.             {
  31.                 clcnxt -= dist(x[s12], x[s1]);
  32.                 clcnxt += dist(x[s12], x[s2]);
  33.             }
  34.             if (s21 >= 0)
  35.             {
  36.                 clcnxt -= dist(x[s21], x[s2]);
  37.                 clcnxt += dist(x[s21], x[s1]);
  38.             }
  39.             if (s22 < x.size())
  40.             {
  41.                 clcnxt -= dist(x[s22], x[s2]);
  42.                 clcnxt += dist(x[s22], x[s1]);
  43.             }
  44.         }
  45.         else
  46.         {
  47.             s1 = rand() % x.size();
  48.             Line a = x[s1];
  49.             a.swapped ^= 1;
  50.             int s11 = s1 - 1;
  51.             int s12 = s1 + 1;
  52.             clcnxt = curclc;
  53.             if (s11 >= 0)
  54.             {
  55.                 clcnxt -= dist(x[s11], x[s1]);
  56.                 clcnxt += dist(x[s11], a);
  57.             }
  58.             if (s12 < x.size())
  59.             {
  60.                 clcnxt -= dist(x[s12], x[s1]);
  61.                 clcnxt += dist(x[s12], a);
  62.             }
  63.         }
  64.         long delta = clcnxt - curclc;
  65.         double prob = exp(-delta / t);
  66.         double rnd = (rand() % 1000000) / 1000000.0;
  67.         if (delta < 0 || rnd < prob)
  68.         {
  69.             if (type)
  70.             {
  71.                 swap(x[s1], x[s2]);
  72.                 hist.push_back(make_pair(s1, s2));
  73.             }
  74.             else
  75.             {
  76.                 x[s1].swapped ^= 1;
  77.                 hist.push_back(make_pair(-1, s1));
  78.             }
  79.             ++cnt;
  80.             curclc = clcnxt;
  81.             if (curclc > maxclc)
  82.             {
  83.                 maxclc = curclc;
  84.                 maxans = cnt;
  85.             }
  86.         }
  87.     }
  88.     cout << cnt << ' '  << maxans << ' ' << hist.size() << endl;
  89.     while (cnt > maxans)
  90.         if (hist[cnt].first == -1)
  91.             x[hist[cnt--].second].swapped ^= 1;
  92.         else
  93.             swap(x[hist[cnt].first], x[hist[cnt--].second]);
  94.     cout << maxans << endl;
  95.     return x;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement