贪心+概率
解决:函数最值、TSP旅行商问题、最小园覆盖、最小求覆盖
模板:
//模拟退火
int eps=1e-8; //终止温度
int T=100; //初始温度
double delta=0.98; //降温系数
double js(int x) ; //评价函数
double now,next;
while(T>eps){
js(next);
js(now);
de=g(next)-g(now);
if(de>=0) now=next; //新状态更好,就受
else if(exp(de/T)>rand()) now=next; //新状态更差,以一定概率接受
T*=delat;
}
hud 2899
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
#include<set>
using namespace std;
const int maxn=1010;
const int INF=0x3fffffff;
typedef long long LL;
//模拟退火
double y;
double eps=1e-8;
double js(double x){
return 6*pow(x,7)+8*pow(x,6.0)+7*pow(x,3)+5*pow(x,2)-y*x;
}
double solve(){
double T=100; //初始温度
double delta=0.98;
double x=50.0; //x的初始值
double now=js(x);
double ans=now; //存储最优值
while(T>eps){
int f[2]={1,-1};
//按照概率改变x
double nex=x+f[rand()%2]*T;
if(nex<101&&nex>=0){
double next=js(nex);
ans=min(ans,next);
if(now-next>eps){
//新的更好
now=next;
x=nex;
}
}
T*=delta; //按照时间降低
}
return ans;
}
int main(){
int tot;
scanf("%d",&tot);
while(tot--){
scanf("%lf",&y);
printf("%.4lf\n",solve());
}
return 0;
}
来源:https://www.cnblogs.com/shirlybaby/p/12392291.html