top
焼きなまし法のテンプレート
const double START_TEMP = 1e4;
const double END_TEMP = 1e-4;
const ll TIME_LIMIT = 5000;
Random rnd;
void solve() {
State now = initialState();
State best = now;
while (timer.check()) {
double next_score = now.get_next_score();
double diff = next_score - now.get_score();
double temp = START_TEMP + (END_TEMP - START_TEMP) * timer.get_time() / TIME_LIMIT;
double prob =
exp(diff / temp);
if (rnd.prob() < prob) {
now.modify();
if (now.get_score() > best.get_score()) {
best = now;
}
}
}
}