hdu 2899 Strange fuction——模擬退火
阿新 • • 發佈:2018-11-01
mes using cti () urn algorithm str turn i++
題目:http://acm.hdu.edu.cn/showproblem.php?pid=2899
還可三分。不過只寫了模擬退火。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<ctime> #include<cmath> #include<cstdlib> #define db double using namespace std; const int nx[6]={0,7,6,3,2,1},c[6]={0,6,8,7,5,1}; const db dc=0.997,eps=1e-17; int T; db y,ans,px; db pw(db x,int k){db ret=1;while(k){if(k&1)ret*=x;x*=x;k>>=1;}return ret;} db calc(db x) { db ret=0; for(int i=1;i<=4;i++)ret+=c[i]*pw(x,nx[i]); ret-=y*x; return ret; } db gtrd(db T){return (rand()*2-RAND_MAX)*T;} voidSA(db T) { db x0=px,pr=ans,x,cr; while(T>eps) { x=x0+gtrd(T); if(x>100)x=100; if(x<0)x=0; cr=calc(x); if(cr<pr||exp((cr-pr)/T)*RAND_MAX<rand()) { if(cr<ans)ans=cr,px=x; x0=x;pr=cr; } T*=dc; } } int main() { srand(time(0)); scanf("%d",&T);while(T--) { scanf("%lf",&y); px=50;ans=calc(px); for(int i=1;i<=2;i++)SA(10000); printf("%.4lf\n",ans); } return 0; }
hdu 2899 Strange fuction——模擬退火