首页 > 代码库 > Hdu5017模拟退火

Hdu5017模拟退火

=。= 之前做过有关果蝇算法的东西,然后发现这俩个其实就是一个东西。。。当时都没想啊,其实想到都不一定能撸对。

#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>#include<math.h>using namespace std;#define eps 1e-9double a, b, c, d, e, f, z1, z2;const int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };const int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };int solvez(double x,double y){    double A = c ,    B = e * x + d * y,    C = a * x * x + b * y * y + f * x * y - 1;    double dlt = B * B - 4 * A * C;    if (dlt < 0) return 0;    z1 = (-B + sqrt(dlt)) / 2 / A,    z2 = (-B - sqrt(dlt)) / 2 / A;    if(z1 * z1 > z2 * z2) swap(z1, z2);    return 1;}double dist(double x, double y, double z){    return sqrt(x * x + y * y + z * z) ;}void gao(double x,double y){    double Rand = 1.0;    int flag = solvez(x , y);    double Min = z1;    while(Rand > eps) {        double x1 = x ; double y1 = y;        for(int i = 0; i < 8; i++) {            double xx = x1 + dx[i] * Rand; double yy = y1 + dy[i] * Rand;            if(!solvez(xx , yy)) continue;            double dis = dist(xx, yy, z1);            if(dis < Min) Min = dis, x = xx, y = yy;        }        Rand *= 0.99;    }    printf("%f\n",Min);}int main(){    while(cin >> a >> b >> c >> d >> e >> f){        gao(0, 0);    }    return 0;}

 

Hdu5017模拟退火