hdu 5017 模拟退火求最值

本文介绍了使用模拟退火算法解决椭圆上原点到距离最小点的距离问题,并提供了优化后的代码实现。

题意:

给一个椭圆:


然后求原点到这个椭圆距离最小的点的距离是多少。



解析:

依旧用模拟退火。

修改了一个地方,初始温度从100改到了1,就行了。

之前的也修改了。


选z的时候,选离远点近的那个点就行了。


代码:

#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#define pb push_back
#define mp make_pair
#define LL long long
#define lson lo,mi,rt<<1
#define rson mi+1,hi,rt<<1|1
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define dec(i,a,b) for(int i=(a); i>=(b); i--)

using namespace std;
const int mod = 1e9 + 7;
const double ee = exp(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 10;
const double pi = acos(-1.0);
const LL iinf = 0x3f3f3f3f3f3f3f3f;

int readT()
{
    char c;
    int ret = 0,flg = 0;
    while(c = getchar(), (c < '0' || c > '9') && c != '-');
    if(c == '-') flg = 1; else ret = c ^ 48;
    while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);
    return flg ? - ret : ret;
}

const double eps = 1e-8;    //搜索停止条件
const double delta = 0.98;  //温度下降速度
const int initT = 1;      //初始温度

int dir[][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
// {1, 1}, {1, -1}, {-1, 1}, {-1, -1}

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

struct Point
{
    double x, y, z;
    Point(){}
    Point(double _x, double _y, double _z)
    {
        x = _x;
        y = _y;
        z = _z;
    }
};

double dist(Point a)
{
    return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}

double getZ(double x, double y)
{
    double A = c;
    double B = e * x + d * y;
    double C = a * x * x + b * y * y + f * x * y - 1;

    double delta = B * B - 4 * A * C;

    if (delta < 0)
    {
        return inf;
    }

    double z1 = (-B + sqrt(delta)) / 2 / A;
    double z2 = (-B - sqrt(delta)) / 2 / A;

    double res = (fabs(z1) < fabs(z2)) ? z1 : z2;
    return res;
}

double solve()
{
    double t = initT;
    double ans = inf;
    Point star(0, 0, 0);
    star.z = getZ(star.x, star.y);

    while (eps < t)
    {
        bool flag = true;
        while (flag)
        {
            flag = false;
            rep(i, 0, 7)
            {
                Point now;
                now.x = star.x + dir[i][0] * t;
                now.y = star.y + dir[i][1] * t;
                now.z = getZ(now.x, now.y);
                if (now.z == inf)
                    continue;
                double t = dist(now);
                if (t < ans)
                {
                    ans = t;
                    star = now;
                    flag = true;
                }
            }
        }
        t *= delta;
    }
    return ans;
}

int main()
{
    #ifdef LOCAL
    FIN;
    #endif // LOCAL
    while (~scanf("%lf%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e, &f))
    {
        printf("%.8lf\n", solve());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值