HDU 2899Strange fuction(模拟退火)

2018-09-18 06:25:10来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

题意

题目链接

求 $F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)$的最小值

Sol

强上模拟退火,注意eps要开大!

/*
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int MAXN = 1e6 + 10;
const double eps = 1e-10, Dlt = 0.98;
int T;
double Y, Best;
double F(double x) {
    return 6 * x * x * x * x * x * x  * x + 8 * x * x * x * x * x * x + 7 * x * x * x + 5 * x * x - Y * x;
}
double getrand(double T) {
    return T * ((rand() << 1) - RAND_MAX);
}
double solve(int id) {
    double x = id, now = F(x);
    Best = min(Best, now);
    for(double T = 101; T > eps; T *= Dlt) {
        double wx = x + getrand(T), wv = F(wx);
        if(wx > eps && (wx - 100 <= eps)) {
            if(wv < Best) x = wx, Best = wv, now = wv;
            if(wv < now || ((exp((wv - now) / T)) * RAND_MAX < rand())) now = wv, x = wx;
        }
    }
}
int main() {
srand((unsigned)time(NULL));
    scanf("%d", &T);
    while(T--) {
        
        Best = 1e20;
        scanf("%lf", &Y);
        int times = 100;
        while(times--) solve(times);
        printf("%.4lf\n", Best);
    }    
    return 0;
}
/*
3
100
200
1000000000
*/

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:next_permutation函数

下一篇:洛谷P2606 [ZJOI2010]排列计数(组合数 dp)