POJ
来源:互联网 发布:加工中心编程视频教程 编辑:程序博客网 时间:2024/06/07 16:59
题目:给定空间的n个点,求最小外接球,输出半径
思路:不知道为什么不管移动后是否最优都要移动,我按照模板上面那样弄就是过不了,一直WA
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3f// 0x3f3f3f3f#define T_min 1e-8//搜索停止阈值#define delta 0.98//温度下降速度const int maxn=105;struct Node{ double x,y,z; Node(double _x=0.0,double _y=0.0,double _z=0.0):x(_x),y(_y),z(_z){}}p[maxn];int n;double Dis(Node a,Node 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));}double get_maxr(Node pos){ double res=0; for(int i=0;i<n;i++){ res=max(res,Dis(pos,p[i])); } return res;}double Search(){ double x=p[0].x,y=p[0].y,z=p[0].z;//随机初始化一个点开始搜索 double T=100000;//初始化温度 double ans=get_maxr(Node(x,y,z));//初始化答案 while(T>T_min){ double dx,dy,dz,mxdis=0; for(int i=0;i<n;i++){ double dis=Dis(Node(x,y,z),p[i]); if(dis>mxdis){ mxdis=dis; dx=(p[i].x-x)/dis; dy=(p[i].y-y)/dis; dz=(p[i].z-z)/dis; } } double cur=get_maxr(Node(x+T*dx,y+T*dy,z+T*dz)); if(cur<=ans){ ans=cur; } x=x+T*dx; y=y+T*dy; z=z+T*dz;// }// else if(exp((cur-ans)/T)<rand()/(RAND_MAX+1.0)){// ans=cur;// x=x+T*dx;// y=y+T*dy;// z=z+T*dz;// } T=T*delta; } return ans;}int main(){ while(~scanf("%d",&n)){ if(!n) break; for(int i=0;i<n;i++) scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z); double ans=Search(); printf("%.5f\n",ans); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- confirm的使用
- notification
- iOS-Jenkins脚本打包上传fir
- 分别使用Matlab和OpenCV标定微距相机
- JS 关于(function( window, undefined ) {})(window)写法的理解
- POJ
- Bootstrap(2)__Bootstrap基础<栅格系统>
- 历史记录+模糊搜索+高亮关键词
- 《奔跑吧Linux》之Linux内核奔跑卷
- Activity启动模式
- 虚拟机安装MAC系统IOS测试环境搭建
- Cygwin设置右键菜单 在当前页面打开
- Intent 和 Intent 过滤器
- Java编程思想之持有对象