Super Star(模拟退火算法)

119 篇文章 1 订阅
113 篇文章 1 订阅

题目链接:Super Star

这个我真不会,只能给出代码了。

//就是下面的我看不懂了,不清楚为什么要这样做,有看懂请留言告知
    p.x+=(pot[id].x-p.x)/r_maxn*t;
    p.y+=(pot[id].y-p.y)/r_maxn*t;
    p.z+=(pot[id].z-p.z)/r_maxn*t;

代码:

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn=50;
struct node
{
    double x,y,z;
} pot[maxn];

double count(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));
}
void reduce(node &p,double &r,double t,int n)
{
    double r_maxn=-inf;//距离
    int id=0;//最远点
    for(int i=0;i<n;i++)//查找与当前球点最远的点,和距离
    {
        double len=count(pot[i],p);
        if(len>r_maxn)
        {
            r_maxn=len;
            id=i;
        }
    }
    r=min(r_maxn,r);//更新半径
    //就是下面的我看不懂了,不清楚为什么要这样做,有看懂请留言告知
    p.x+=(pot[id].x-p.x)/r_maxn*t;
    p.y+=(pot[id].y-p.y)/r_maxn*t;
    p.z+=(pot[id].z-p.z)/r_maxn*t;
    return ;
}
double solve(int n)
{
    double base=0.98;//相当于温度下降的速率吧
    double t=100;//初始温度
    double ans=1e-8;//最终的温度
    double r=inf;//记录最终答案
    node p=pot[0];//随便取一个点
    while(t>ans)
    {
        reduce(p,r,t,n);//查找最短的半径,向球心靠近
        t*=base;
    }
    return r;
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0; i<n; i++)
            scanf("%lf%lf%lf",&pot[i].x,&pot[i].y,&pot[i].z);
        double kp=solve(n);
        printf("%.5lf\n",kp);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值