Poj_2069_Super Star(模拟退火算法)
2016-07-03 20:32
531 查看
Super Star
Description
During a voyage of the starship Hakodate-maru (see Problem 1406), researchers found strange synchronized movements of stars. Having heard these observations, Dr. Extreme proposed a theory of "super stars". Do not take this term as a description of actors or
singers. It is a revolutionary theory in astronomy.
According to this theory, starts we are observing are not independent objects, but only small portions of larger objects called super stars. A super star is filled with invisible (or transparent) material, and only a number of points inside or on its surface
shine. These points are observed as stars by us.
In order to verify this theory, Dr. Extreme wants to build motion equations of super stars and to compare the solutions of these equations with observed movements of stars. As the first step, he assumes that a super star is sphere-shaped, and has the smallest
possible radius such that the sphere contains all given stars in or on it. This assumption makes it possible to estimate the volume of a super star, and thus its mass (the density of the invisible material is known).
You are asked to help Dr. Extreme by writing a program which, given the locations of a number of stars, finds the smallest sphere containing all of them in or on it. In this computation, you should ignore the sizes of stars. In other words, a star should be
regarded as a point. You may assume the universe is a Euclidean space.
Input
The input consists of multiple data sets. Each data set is given in the following format.
n
x1 y1 z1
x2 y2 z2
. . .
xn yn zn
The first line of a data set contains an integer n, which is the number of points. It satisfies the condition 4 <= n <= 30.
The location of n points are given by three-dimensional orthogonal coordinates: (xi, yi, zi) (i = 1, ..., n). Three coordinates of a point appear in a line, separated by a space character. Each value is given by a decimal fraction, and is between 0.0 and 100.0
(both ends inclusive). Points are at least 0.01 distant from each other.
The end of the input is indicated by a line containing a zero.
Output
For each data set, the radius of the smallest sphere containing all given points should be printed, each in a separate line. The printed values should have 5 digits after the decimal point. They may not have an error greater than 0.00001.
Sample Input
Sample Output
Source
Japan 2001
放假了,学点有趣的东西。模拟退火算法。。。
这道题不是随机找点,而是往最远点移动。算是长见识了。
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4640 | Accepted: 1160 | Special Judge |
During a voyage of the starship Hakodate-maru (see Problem 1406), researchers found strange synchronized movements of stars. Having heard these observations, Dr. Extreme proposed a theory of "super stars". Do not take this term as a description of actors or
singers. It is a revolutionary theory in astronomy.
According to this theory, starts we are observing are not independent objects, but only small portions of larger objects called super stars. A super star is filled with invisible (or transparent) material, and only a number of points inside or on its surface
shine. These points are observed as stars by us.
In order to verify this theory, Dr. Extreme wants to build motion equations of super stars and to compare the solutions of these equations with observed movements of stars. As the first step, he assumes that a super star is sphere-shaped, and has the smallest
possible radius such that the sphere contains all given stars in or on it. This assumption makes it possible to estimate the volume of a super star, and thus its mass (the density of the invisible material is known).
You are asked to help Dr. Extreme by writing a program which, given the locations of a number of stars, finds the smallest sphere containing all of them in or on it. In this computation, you should ignore the sizes of stars. In other words, a star should be
regarded as a point. You may assume the universe is a Euclidean space.
Input
The input consists of multiple data sets. Each data set is given in the following format.
n
x1 y1 z1
x2 y2 z2
. . .
xn yn zn
The first line of a data set contains an integer n, which is the number of points. It satisfies the condition 4 <= n <= 30.
The location of n points are given by three-dimensional orthogonal coordinates: (xi, yi, zi) (i = 1, ..., n). Three coordinates of a point appear in a line, separated by a space character. Each value is given by a decimal fraction, and is between 0.0 and 100.0
(both ends inclusive). Points are at least 0.01 distant from each other.
The end of the input is indicated by a line containing a zero.
Output
For each data set, the radius of the smallest sphere containing all given points should be printed, each in a separate line. The printed values should have 5 digits after the decimal point. They may not have an error greater than 0.00001.
Sample Input
4 10.00000 10.00000 10.00000 20.00000 10.00000 10.00000 20.00000 20.00000 10.00000 10.00000 20.00000 10.00000 4 10.00000 10.00000 10.00000 10.00000 50.00000 50.00000 50.00000 10.00000 50.00000 50.00000 50.00000 10.00000 0
Sample Output
7.07107 34.64102
Source
Japan 2001
放假了,学点有趣的东西。模拟退火算法。。。
/* * J(y):在状态y时的评价函数值 * Y(i):表示当前状态 * Y(i+1):表示新的状态 * r: 用于控制降温的快慢 * T: 系统的温度,系统初始应该要处于一个高温的状态 * T_min :温度的下限,若温度T达到T_min,则停止搜索 */ while( T > T_min ) { dE = J( Y(i+1) ) - J( Y(i) ) ; if ( dE >=0 ) //表达移动后得到更优解,则总是接受移动 Y(i+1) = Y(i) ; //接受从Y(i)到Y(i+1)的移动 else { if ( exp( dE/T ) > random( 0 , 1 ) ) // 函数exp( dE/T )的取值范围是(0,1) ,dE/T越大,则exp( dE/T )也 Y(i+1) = Y(i) ; //接受从Y(i)到Y(i+1)的移动 } T = r * T ; //降温退火 ,0<r<1 。r越大,降温越慢;r越小,降温越快 //若r过大,则搜索到全局最优解的可能会较高,但搜索的过程也就较长。若r过小,则搜索的过程会很快,但最终可能会达到一个局部最优值 i ++ ; }
这道题不是随机找点,而是往最远点移动。算是长见识了。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <stack> #include <bitset> #include <queue> #include <set> #include <map> #include <string> #include <algorithm> #define Si(a) scanf("%d",&a) #define Sl(a) scanf("%lld",&a) #define Sd(a) scanf("%lf",&a) #define Ss(a) scanf("%s",a) #define Pi(a) printf("%d\n",(a)) #define Pl(a) printf("%lld\n",(a)) #define Pd(a) printf("%lf\n",(a)) #define Ps(a) printf("%s\n",(a)) #define W(a) while(a--) #define mem(a,b) memset(a,(b),sizeof(a)) #define FOP freopen("data.txt","r",stdin) #define inf 1e99 #define maxn 1000010 #define mod 1000000007 #define PI acos(-1.0) #define LL long long #define eps 1e-8 using namespace std; struct point { double x,y,z; } P[100]; double getLen(point a,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 Search() { point s=P[0]; double ans=999999; double T=100; double delta=0.98; while(T>eps) { int k=0; for(int i=0; i<n; i++) { if(getLen(s,P[i])>getLen(s,P[k])) k=i; } double d=getLen(s,P[k]); ans=min(ans,d); s.x+=(P[k].x-s.x)/d*T; s.y+=(P[k].y-s.y)/d*T; s.z+=(P[k].z-s.z)/d*T; T*=delta; } return ans; } int main() { while(scanf("%d",&n)&&n!=0) { for(int i=0; i<n; i++) { scanf("%lf%lf%lf",&P[i].x,&P[i].y,&P[i].z); } printf("%.5f\n",Search()); } return 0; }
相关文章推荐
- Bzoj3140:[Hnoi2013]消毒:二分图
- private,protected,public,static 作用域
- 安卓中的自定义ListView学习总结
- LeetCode——two sum of integer
- LeetCode Two Sum(C/JS)
- 【leetcode】15. 3Sum
- VS2012C++学习笔记1(第1-2章)
- genymotion unable to start the device的解决方案
- 梯度法,牛顿法,拟牛顿法
- java之两个字符串的比较
- 蘭州拉面
- (有参考基因组)植物转录组分析之一数据处理
- java编程习惯
- Swift基础(九):错误处理
- Java算法之递归打破和在真实项目中的使用实例
- JAVA 集合性能特性汇总
- 追踪监听(TraceListener)
- js总结
- 进程
- 二叉树遍历的非递归实现