zoukankan      html  css  js  c++  java
  • hdu 5017 模拟退火算法

    hdu  5017  
    http://blog.csdn.net/mypsq/article/details/39340601
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    
    
    const int D[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
    double a, b, c, d, e, f;
    const double INF = 1e9;
    const double eps = 1e-8;
    
    
    double dis(double x, double y, double z) {
    	return sqrt(x * x + y * y + z * z);
    }
    
    
    double cal(double x, double y) {//已知x  y  求z
    	double A = c;
    	double B = d * y + e * x;
    	double C = f * x * y + a * x * x + b * y * y - 1;
    	double del = B * B - 4 * A * C;
    	if (del < 0) return 1e10;
    	del = sqrt(del);
    	double z1 = (-B + del) / A / 2;
    	double z2 = (-B - del) / A / 2;
    	if (dis(x, y, z1) < dis(x, y, z2)) return z1;
    	return z2;
    }
    
    
    double solve() {
    	double x = 0, y = 0, z = sqrt(1.0 / c);
    	double step = 1.0, r = 0.99;//搜索半径、降温快慢
    	while (step > eps) {
    		for (int i = 0; i < 8; i++) {//八个方向
    			double xx = x + step * D[i][0];//通过step不断缩小搜索范围
    			double yy = y + step * D[i][1];
    			double zz = cal(xx, yy);//已知x、y  求z
    			if (zz > INF) continue;
    			if (dis(xx, yy, zz) < dis(x, y, z)) {
    				x = xx; y = yy; z = zz;//移动后的到最优解接受移动
    			}
    		}
    		step *= r;//缩小搜索半径
    	}
    	return dis(x, y, z);
    }
    
    
    int main() {
    	while (~scanf("%lf%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e, &f)) {
    		printf("%.7lf
    ", solve());
    	}
    	return 0;
    }
    

  • 相关阅读:
    我要变牛逼
    java web
    导师选择
    2.1进程
    来到博客园写东西的第一天
    简单的页面布局
    html5
    第一个servlet程序
    java2D
    字节流 文件字节流 缓冲字节流
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410623.html
Copyright © 2011-2022 走看看