首页 > 代码库 > 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模拟退火
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。