Ellipsoid - HDU 5017---模拟退火算法

题目链接:https://vjudge.net/problem/HDU-5017

第一次遇到这个算法,听这个名有点奇怪。

下面附上一些对这个算法解释的博客,可以学习。

https://www.cnblogs.com/ranjiewen/p/6084052.html

https://blog.csdn.net/daaikuaichuan/article/details/81381875

https://blog.csdn.net/weixin_42398658/article/details/84031235

                                                                  Ellipsoid

Given a 3-dimension ellipsoid(椭球面) 


your task is to find the minimal distance between the original point (0,0,0) and points on the ellipsoid. The distance between two points (x 1,y 1,z 1) and (x 2,y 2,z 2) is defined as

Input

There are multiple test cases. Please process till EOF. 

For each testcase, one line contains 6 real number a,b,c(0 < a,b,c,< 1),d,e,f (0 ≤ d,e,f < 1), as described above. It is guaranteed that the input data forms a ellipsoid. All numbers are fit in double. 

Output

For each test contains one line. Describes the minimal distance. Answer will be considered as correct if their absolute error is less than 10 -5.

Sample Input

1 0.04 0.01 0 0 0

Sample Output

1.0000000

 题目大意:在椭球面上找一个点使得到(0,0,0)的距离最小,输出这个最小距离。

模拟退火法来解决这个问题。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<string>
#define cla(a, sum) memset(a, sum, sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 1e8;
const double eps = 1e-8;
int dx[8]={0,0,1,1,1,-1,-1,-1};
int dy[8]={1,-1,0,1,-1,1,-1,0};
double a,b,c,d,e,f;

//计算距离 
double getsum(double x,double y,double z)
{
	return sqrt(x*x+y*y+z*z);
}

//求解z坐标 
double dis(double x,double y){
	double A=c;
	double B=d*y+e*x;
	double C=a*x*x+b*y*y+f*x*y-1.0;
	double dert=B*B-4.0*A*C;
	if(dert<0.0)return inf+10.0;
	dert=sqrt(dert);
	double z1=(-B+dert)/(2.0*A);
	double z2=(-B-dert)/(2.0*A);
	if(getsum(x,y,z1)<getsum(x,y,z2)){
		return z1;
	}
	return z2;
}

double solve()
{
	double x=0,y=0,z=sqrt(1.0/c);
	double step=1.0,rate=0.99;//以rate的速率递减寻找的范围
	int i,j;
	double nx,ny,nz;
	while(step>eps){
		for(i=0;i<8;i++)
		{
			nx=x+dx[i]*step;
			ny=y+dy[i]*step;
			nz=dis(nx,ny);
			if(nz>=inf)continue;
			if(getsum(nx,ny,nz)<getsum(x,y,z)){//更新旧值 
				x=nx;
				y=ny;
				z=nz;
			}
		}
		step*=rate;//之后以更小的移动概率寻找新解
	}
	return getsum(x,y,z);
}

int main()
{
	while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f)!=EOF){
		printf("%.7f\n",solve());
	}
	return 0;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值