poj 2069 Super Star 模拟退火

题目大意:

给定三位空间上的n((n leq 30))个点,求最小的球覆盖掉所有的点.

题解:

貌似我们可以用类似于二维平面中的随机增量法瞎搞一下
但是我不会怎么搞
所以我们模拟退火就好了啊QAQ

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
	x=0;char ch;bool flag = false;
	while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
	while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 45;
const double eps = 1e-15;
const double det = 0.99;
struct Point{
	double x,y,z;
	Point(const double &a=0,const double &b=0,const double &c=0){
		x=a;y=b;z=c;
	}
};
inline double dis(const Point &a,const Point &b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
int n;double ans = 1e100;
Point p[maxn];
inline double f(const Point &x){
	double ret = 0;
	for(int i=1;i<=n;++i) ret = max(ret,dis(x,p[i]));
	if(ret < ans) ans = ret;
	return ret;
}
inline double ran(){
	return (rand() % 1000 + 1)/1000.0;
}
Point nw;
int main(){
	srand(2333);
	while(1){
		read(n);if(n == 0) break;
		nw.x = nw.y = nw.z = 0;
		ans = 1e100;
		for(int i=1;i<=n;++i){
			scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z);
		}
		double T = 100.0,x;
		double dist;int pos;
		while(T > eps){
			dist = 0.0;
			for(int i=1;i<=n;++i){
				if(dist < dis(nw,p[i])){
					pos = i;dist = dis(nw,p[i]);
				}
			}
			Point nx(
				nw.x+(p[pos].x-nw.x)/dist*T,
				nw.y+(p[pos].y-nw.y)/dist*T,
				nw.z+(p[pos].z-nw.z)/dist*T
				);
			x = f(nw) - f(nx);
			if(x > 0 || exp(x/T) > ran()) nw = nx;
			T *= det;
		}
		printf("%.5lf
",ans);
	}
	getchar();getchar();
	return 0;
}
原文地址:https://www.cnblogs.com/Skyminer/p/6438262.html