hdu 5017 Ellipsoid 模拟退火

Ellipsoid

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2228    Accepted Submission(s): 825
Special Judge


Problem Description
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
 

Source

题意:

给定几个点  a,b,c,d,e,f的值,求在椭圆上的点x,y,z,使得式子 sqrt(x*x+y*y+z*z)的值最小,并输出该值。


思路:
第一题模拟退火的题目;
关于模拟退火,有很多讲的很好的博客,例如http://blog.sina.com.cn/s/blog_a3a7ea8201014btj.html

理解了思路,写出代码还是相对容易的,重要的是确定步长和步长变化的数值大小;


代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

double a,b,c,d,e,f;

const double eps= 1e-8;

int dx[8] = {0,0,1,-1,1,-1,1,-1};
int dy[8] = {1,-1,0,0,1,1,-1,-1};

double dis (double x,double y,double z){
    return sqrt(x*x+y*y+z*z);
}


double calz (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;

    double delta = B*B-4*A*C;
    if(delta<0){
        return -1;
    }
    double z1=(-1.0*B+sqrt(delta))/(2*A);
    double z2=(-1.0*B-sqrt(delta))/(2*A);

    if(dis(x,y,z1)<dis(x,y,z2))
        return z1;
    else
        return z2;
}


double  Simulated_Annealing(){
    double step=1.0;
    double rate=0.99;

    double ans = 0x3f3f3f3f;
    double fx=0,fy=0;
    double fz=sqrt(1.0/c);

    while(step>eps){

        for(int k=0;k<8;k++)
        {
            double kx = fx + step*dx[k];
            double ky = fy + step*dy[k];
            double kz = calz(kx,ky);
            if(kz ==-1) continue;
            if(dis(kx,ky,kz) < dis(fx,fy,fz))
            {
                fx = kx,fy = ky,fz = kz;
            }
        }

        step*=rate;
    }
    return dis(fx,fy,fz);
}

int main(){

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


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值