hdu 5017 ellipsoid 退火大法

题目链接:点击打开链接

题意:给形如ax^2+by^2+cz^2+dxy+eyz+fxz=1的椭球面方程,求椭球面距离原点最近的距离。

思路:可知由不是最优到最优之间有一条路径是递减的,退火大法好!!

cpp:

#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define INF 1e10
const double eps=1e-4;
int dx[8]={0,1,0,-1,1,1,-1,-1},dy[8]={1,0,-1,0,-1,1,-1,1};
double a,b,c,d,e,f;
double get_dis(double x, double y, double z) {
	return sqrt(x*x+y*y+z*z);
}
double get_z(double x,double y) {
	double A,B,C; 
	A=c,B=e*x+d*y,C=a*x*x+b*y*y+f*x*y-1;
	double temp=B*B-4*A*C;
	if (temp<0) return INF;
	double ret1,ret2; 
	ret1=(-B+sqrt(temp))/2/A;
	ret2=(-B-sqrt(temp))/2/A;
	if (fabs(ret1)<fabs(ret2)) return ret1;
	else return ret2;
}
void simulated_annealing() {
	double step,ret,x,y,z;
	step = 1;
	ret=INF;
	x=0,y=0,z=get_z(x,y);
	while (step>eps) {
		for (int i=0;i<8;i++) {
			double nx,ny,nz;
			nx=x+dx[i]*step;
			ny=y+dy[i]*step;
			nz=get_z(nx,ny);
			if (nz>=INF) continue;
			double tp=get_dis(nx,ny,nz);
			if (tp<ret) {
				x=nx;
				y=ny;
				z=nz;
				ret=tp;
			}
		}
		step*=0.97;
	}
	printf("%.8f\n",ret);
}
int main() {
	while (~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f))
		simulated_annealing();
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值