其他分享
首页 > 其他分享> > HDU2899 Strange fuction(模拟退火)

HDU2899 Strange fuction(模拟退火)

作者:互联网

模拟退火模板,按比例接受

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const double eps=1e-8;
double ans;
double y;
double cal(double x){return 6*pow(x,7.0)+8*pow(x,6.0)+7*pow(x,3.0)+5*pow(x,2.0)-y*x;}
int f[2]={1,-1};
double solve(double tmp){
    double res=1e18;
    double cur;
    double sx=50.0;
    cur=cal(sx);
    double dc=0.98;
    res=min(res,cur);
    while(tmp>eps){
        double nx=sx+f[rand()%2]*tmp;
        if(nx>100.0||nx<0.0);
        else{
            double nxd=cal(nx);
            res=min(res,nxd);
            if(cur-nxd>eps)sx=nx,cur=nxd;
            else if(exp(-(nxd-cur)/tmp)*RAND_MAX>rand()){
                sx=nx,cur=nxd;
            }
        }
        tmp*=dc;
    }
    return res;
}
int main(){
    //ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        cin>>y;
        ans=solve(100);
        printf("%.4f\n",ans);
    }
}
View Code

 

标签:tmp,fuction,Strange,cur,int,double,sx,模拟退火,pow
来源: https://www.cnblogs.com/ctyakwf/p/13849840.html