生活随笔
收集整理的這篇文章主要介紹了
poj 2069 Super Star 模拟退火
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目大意:
給定三位空間上的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\n",ans);}getchar();getchar();return 0;
}
轉(zhuǎn)載于:https://www.cnblogs.com/Skyminer/p/6438262.html
總結(jié)
以上是生活随笔為你收集整理的poj 2069 Super Star 模拟退火的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。