HDU 5017 Ellipsoid 模拟退火

网赛的时候感觉可以用模拟退火搞但是不会写,今天学了一下感觉模拟退火本身也不是很难= =

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;
const double eps = 1e-8;
const double r = 0.99;	//降温速度
const int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
const int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
double a, b, c, d, e, f;

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

//已知x,y,求z
double getz(double x, double y) {
	double A = c, B = e * x + d * y,
		C = a * x * x + b * y * y + f * x * y - 1;
	double delta = B * B - 4 * A * C;
	if (delta < 0) return 1e60;
	double z1 = (-B + sqrt(delta)) / 2 / A,
		z2 = (-B - sqrt(delta)) / 2 / A;
	if (z1 * z1 < z2 * z2) return z1;
	else return z2;
}

double solve() {
	//模拟退火
	double step = 1;	//步长
	double x = 0, y = 0, z;
	while (step > eps) {
		z = getz(x, y);
		for (int i = 0; i < 8; i++) {
			double nx = x + dx[i] * step,
				ny = y + dy[i] * step,
				nz = getz(nx, ny);
			if (nz > 1e30) continue;
			if (dis(nx, ny, nz) < dis(x, y, z)) {
				x = nx; y = ny; z = nz;
			}
		}
		step *= r;
	}
	return dis(x, y, z);
}

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

  

时间: 2024-10-19 03:20:41

HDU 5017 Ellipsoid 模拟退火的相关文章

HDU - 5017 Ellipsoid(模拟退火法)

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 (x1,y1,z1) and (x2,y2,z2) is defined as  Input There are

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}}; dou

hdu 5017 Ellipsoid(三分)

题目链接:hdu 5017 Ellipsoid 题目大意:给定一个面的方程,问在面上距离原点的最小值. 解题思路:三分套三分,先三分x,对于每个x,三分y,求出的最优解作为当前x的值. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const double INF = 10000; const double eps

HDU 5017 Ellipsoid (计算几何,模拟退火)

Ellipsoid 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 (x1,y1,z1) and (x2,y2,z2) is defined as  Input

hdu 5017 Ellipsoid(模拟退火)

Ellipsoid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1140    Accepted Submission(s): 412 Special Judge Problem Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minim

hdu 5017 Ellipsoid(西安网络赛 1011)

Ellipsoid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 850    Accepted Submission(s): 271 Special Judge Problem Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minima

2014 ACM/ICPC Asia Regional Xi&#39;an Online(HDU 5007 ~ HDU 5017)

题目链接 A题:(字符串查找,水题) 题意 :输入字符串,如果字符串中包含“ Apple”, “iPhone”, “iPod”, “iPad” 就输出 “MAI MAI MAI!”,如果出现 “Sony” 就输出“SONY DAFA IS GOOD!” ,大小写敏感. 思路 : 字符串查找,水题. 1 #include <string.h> 2 #include <stdio.h> 3 #include <iostream> 4 5 using namespace st

hdu 5017 模拟退火

题意:给出椭球面的立体解析式,要求椭球面上距离原点最近的点的距离 sol:这题要想推公式就??????...[可以试试二元函数求极值 一种比较普遍的解法是模拟退火 模拟退火的解释可以参考这儿:http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html 模拟退火模板get: 1 double sa() //Simulated_Annealing 2 { 3 double x = 0,y = 0,z = sqrt(1.0/C); //当前

HDOJ 5017 Ellipsoid

第一次尝试模拟退火..... Ellipsoid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 916    Accepted Submission(s): 305 Special Judge Problem Description Given a 3-dimension ellipsoid(椭球面) your task is to