Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<Line> FSPGraph(vector<Line> x)
- {
- double t = 10000;
- long curclc = calc(x);
- long maxclc = curclc;
- int maxans = 0;
- vector<pair<int, int>> hist;
- int i, cnt = 0;
- for (i = 0; t > 0.00000001; ++i)
- {
- t *= 0.99999;
- bool type = rand() & 1;
- long clcnxt = 0;
- int s1 = -1, s2 = -1;
- if (type)
- {
- s1 = rand() % x.size();
- s2 = rand() % x.size();
- int s11 = s1 - 1;
- int s12 = s1 + 1;
- int s21 = s2 - 1;
- int s22 = s2 + 1;
- clcnxt = curclc;
- if (s11 >= 0)
- {
- clcnxt -= dist(x[s11], x[s1]);
- clcnxt += dist(x[s11], x[s2]);
- }
- if (s12 < x.size())
- {
- clcnxt -= dist(x[s12], x[s1]);
- clcnxt += dist(x[s12], x[s2]);
- }
- if (s21 >= 0)
- {
- clcnxt -= dist(x[s21], x[s2]);
- clcnxt += dist(x[s21], x[s1]);
- }
- if (s22 < x.size())
- {
- clcnxt -= dist(x[s22], x[s2]);
- clcnxt += dist(x[s22], x[s1]);
- }
- }
- else
- {
- s1 = rand() % x.size();
- Line a = x[s1];
- a.swapped ^= 1;
- int s11 = s1 - 1;
- int s12 = s1 + 1;
- clcnxt = curclc;
- if (s11 >= 0)
- {
- clcnxt -= dist(x[s11], x[s1]);
- clcnxt += dist(x[s11], a);
- }
- if (s12 < x.size())
- {
- clcnxt -= dist(x[s12], x[s1]);
- clcnxt += dist(x[s12], a);
- }
- }
- long delta = clcnxt - curclc;
- double prob = exp(-delta / t);
- double rnd = (rand() % 1000000) / 1000000.0;
- if (delta < 0 || rnd < prob)
- {
- if (type)
- {
- swap(x[s1], x[s2]);
- hist.push_back(make_pair(s1, s2));
- }
- else
- {
- x[s1].swapped ^= 1;
- hist.push_back(make_pair(-1, s1));
- }
- ++cnt;
- curclc = clcnxt;
- if (curclc > maxclc)
- {
- maxclc = curclc;
- maxans = cnt;
- }
- }
- }
- cout << cnt << ' ' << maxans << ' ' << hist.size() << endl;
- while (cnt > maxans)
- if (hist[cnt].first == -1)
- x[hist[cnt--].second].swapped ^= 1;
- else
- swap(x[hist[cnt].first], x[hist[cnt--].second]);
- cout << maxans << endl;
- return x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement