HDU 5017 Ellipsoid(西安网络赛K题)

HDU 5017 Ellipsoid

题目链接

思路:模拟退火大法好!

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int D[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
double a, b, c, d, e, f;
const double INF = 1e9;
const double eps = 1e-8;

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

double cal(double x, double y) {
	double A = c;
	double B = d * y + e * x;
	double C = f * x * y + a * x * x + b * y * y - 1;
	double del = B * B - 4 * A * C;
	if (del < 0) return 1e10;
	del = sqrt(del);
	double z1 = (-B + del) / A / 2;
	double z2 = (-B - del) / A / 2;
	if (dis(x, y, z1) < dis(x, y, z2)) return z1;
	return z2;
}

double solve() {
	double x = 0, y = 0, z = sqrt(1.0 / c);
	double step = 1.0, r = 0.99;
	while (step > eps) {
		for (int i = 0; i < 8; i++) {
			double xx = x + step * D[i][0];
			double yy = y + step * D[i][1];
			double zz = cal(xx, yy);
			if (zz > INF) continue;
			if (dis(xx, yy, zz) < dis(x, y, z)) {
				x = xx; y = yy; z = zz;
			}
		}
		step *= r;
	}
	return dis(x, y, z);
}

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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值