备战NOIP——模板复习8
程序员文章站
2022-03-23 11:17:19
...
这里只有模板,并不作讲解,仅为路过的各位做一个参考以及用做自己复习的资料,转载注明出处。
模拟退火(SA)
/*Copyright: Copyright (c) 2018
*Created on 2018-10-28
*Author: 十甫
*Version 1.0
*Title: SA
*Time: 5 mins
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const double delta = 0.999;
const double zero = 1e-3;
const double eps = 1e-7;
double calc(double k) {
}
int main() {
srand(time(NULL));
int n;
scanf("%d", &n);
while(n--) {
//scanf()
double T = 1000.0;
double ans = 0.0, x = 0.0;
while(T > zero) {
double x_ = x + ((rand() * 2) - RAND_MAX) * T;
double k = calc(x_) - ans;
if(k > 0) {
ans = k, x = x_;
} else {
if(exp(-k / T) * RAND_MAX > rand()) {
x = x_;
}
}
T *= delta;
}
printf("%.5lf\n", ans);
}
return 0;
}