# Country Meow（模拟退火入门）

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<cstdlib>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e9+7;
const int maxn=1e2+50;
const double StartT=50000;//初始温度 1000也可以过这道题
const double rate=0.99;//退火系数 一般0.97-0.99
const double eps=1e-6;//精度 可以在题目要求的小数点后面位数多加3左右
const double FINF=1e18;
/**

rate=0.98
T=1000 1026次
T=20000 1175次
T=50000 1220次

rate=0.99
T=50000 2452
**/
int N;
struct Node
{
double x,y,z;
Node(){}
Node(double x1,double y1,double z1)
{
x=x1;y=y1;z=z1;
}
}a[maxn];
double dist(Node n1,Node n2)
{
return sqrt((n1.x-n2.x)*(n1.x-n2.x)+(n1.y-n2.y)*(n1.y-n2.y)+(n1.z-n2.z)*(n1.z-n2.z));
}
void solve()
{
//    int sum=0;
double ans=FINF;
//    cout<<ans<<endl;
double T=StartT;
Node now=Node(0,0,0);//随机选取一个点
Node max_p=a[1];//离当前最远的点
while(T>eps)
{
//        sum++;
for(int i=1;i<=N;i++)//寻找里当前最远的点
{
if(dist(a[i],now)>=dist(max_p,now))
{
max_p=a[i];
}
}
ans=min(ans,dist(now,max_p));
now.x=(now.x+(max_p.x-now.x)*(T/StartT));
now.y=(now.y+(max_p.y-now.y)*(T/StartT));
now.z=(now.z+(max_p.z-now.z)*(T/StartT));
T=T*rate;
}
//    cout<<"sum:"<<sum<<endl;
printf("%.15lf\n",ans);
}
int main()
{
scanf("%d",&N);
for(int i=1;i<=N;i++)
{
scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z);
}
solve();
return 0;
}

posted @ 2019-10-14 21:11  执||念  阅读(413)  评论(0编辑  收藏  举报