首页 > 代码库 > HDU 5017 Ellipsoid 模拟退火
HDU 5017 Ellipsoid 模拟退火
网赛的时候感觉可以用模拟退火搞但是不会写,今天学了一下感觉模拟退火本身也不是很难= =
#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;const double eps = 1e-8;const double r = 0.99; //降温速度const int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };const int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };double a, b, c, d, e, f;double dis(double x, double y, double z) { return sqrt(x * x + y * y + z * z);}//已知x,y,求zdouble getz(double x, double y) { double A = c, B = e * x + d * y, C = a * x * x + b * y * y + f * x * y - 1; double delta = B * B - 4 * A * C; if (delta < 0) return 1e60; double z1 = (-B + sqrt(delta)) / 2 / A, z2 = (-B - sqrt(delta)) / 2 / A; if (z1 * z1 < z2 * z2) return z1; else return z2;}double solve() { //模拟退火 double step = 1; //步长 double x = 0, y = 0, z; while (step > eps) { z = getz(x, y); for (int i = 0; i < 8; i++) { double nx = x + dx[i] * step, ny = y + dy[i] * step, nz = getz(nx, ny); if (nz > 1e30) continue; if (dis(nx, ny, nz) < dis(x, y, z)) { x = nx; y = ny; z = nz; } } step *= r; } return dis(x, y, z);}int main() { while (scanf("%lf%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e, &f) != EOF) { printf("%.8f\n", solve()); } return 0;}
HDU 5017 Ellipsoid 模拟退火
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。