hdu 5017 Ellipsoid(模拟退火)
2015-09-02 15:20
330 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5017
题意:给定一个椭球面方程,让你找到椭球面上距离原点最近的点,并求出距离
思路:原本想推出公式,结果需要解一元三次方程,放弃。。。百度发现了这个模拟退火的算法
具体算法详见链接:http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html
这也提供给我们一种解决三元二次方程的方法
代码:
题意:给定一个椭球面方程,让你找到椭球面上距离原点最近的点,并求出距离
思路:原本想推出公式,结果需要解一元三次方程,放弃。。。百度发现了这个模拟退火的算法
具体算法详见链接:http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html
这也提供给我们一种解决三元二次方程的方法
代码:
#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); } ///c*z*z+(d * y + e * x)*z+(f * x * y + a * x * x + b * y * y - 1)=0 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\n", solve()); } return 0; }
相关文章推荐
- string 与char* char[]之间的转换
- 如何用Docker安装Eclipse Che和Codenvy
- easyui 列表datagrid初始化两次的问题
- Java NIO:NIO概述
- AndroidStudio使用——如何导入jar包
- Ajax核心5步
- 、求1000!的结果中包含多少个0?注:1000! = 1×2×3×4×5×...×999×1000 *
- Unity 2D游戏开发教程之使用脚本实现游戏逻辑
- 正则匹配常用案例
- 8.11.2 Table Locking Issues 表锁发生
- 链家旗下自如某站一个有意思的文件包含到简单内网渗透(本地文件包含getshell技巧)
- android中判断service是否运行
- databus编译: Execution failed for task ':databus-core:databus-core-impl:compileJava'.
- J2EE、J2SE、J2ME、JDK各个击破(概念级)
- 增加<em>标签
- 数据结构中常见的树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)
- Spark 下操作 HBase(1.0.0 新 API)
- 判断是不是中文
- Java 字符串和时间互相转化 +时间戳
- 奇葩继承