hdu 5017 模拟退火求最值

来源:互联网 发布:模拟男朋友的软件 编辑:程序博客网 时间:2024/06/03 12:54

题意:

给一个椭圆:


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



解析:

依旧用模拟退火。

修改了一个地方,初始温度从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;}


0 0