模拟退火笔记-Simulated Annealing

Simulated Annealing

A [JSOI2004]平衡点

题目链接

Description

Solution

Code

[JSOI2004]平衡点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<bits/stdc++.h>
using namespace std;

constexpr int MAXN = 1e3 + 3;
constexpr double down = 0.996;

int n;
pair<pair<int, int>, int> obj[MAXN];
double x, y, Ep;

inline double energy(double x, double y) { //重力势能越小越稳定
double E = 0;
for (int i = 0; i < n; i++) {
double dx = x - obj[i].first.first;
double dy = y - obj[i].first.second;
E += sqrt(dx * dx + dy * dy) * obj[i].second;
}
return E;
}

inline void SA() { //SimulateAnneal
double T = 3000;
while (T > 1e-14) {
double randx = x + (rand() * 2 - RAND_MAX) * T;
double randy = y + (rand() * 2 - RAND_MAX) * T;
double E = energy(randx, randy);
double delta = E - Ep;
if (delta < 0) { //E更小 答案更优
x = randx, y = randy, Ep = E;
}
else if (exp(-delta / T) * RAND_MAX > rand()) { //根据多项式概率接受
x = randx, y = randy;
}
T *= down;
}
}

int main() {
std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> obj[i].first.first >> obj[i].first.second >> obj[i].second;
x += obj[i].first.first, y += obj[i].first.second;
}
x /= n, y /= n, Ep = energy(x, y);
for (int i = 0; i < 4; i++) SA();
cout << setprecision(3) << fixed << x << " " << y << endl;
return 0;
}