luogu P4035 [JSOI2008]球形空间产生器

[返回模拟退火略解]

题目描述

今有 n + 1 n+1 n+1 n n n 维的点,它们都在一个球上。求它们所在球的球心。

Solution 4035 \text{Solution 4035} Solution 4035

最近学 SA,想用这题练练手。发现参数很难调。考场打的话,得分区间 [ 0 , 100 ] [0,100] [0,100](手动滑稽)
c a l c ( X ) calc(X) calc(X) 表示题目给出的 n + 1 n+1 n+1 个点中,与 X X X 的最大距离和最小距离的差。容易得到, c a l c ( X ) = 0 calc(X)=0 calc(X)=0 时, X X X 为球心。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>

#define reg register

int n;
struct node{
	double a[15];
}as,cc,e[20],o;
double ans=1e18;
double c[15],ss;

double calc(node x){
	double mac=0,mic=1e17,sum;
	for(reg int i=1;i<=n+1;++i){
		sum=0.0;
		for(reg int j=1;j<=n;++j)
			sum+=(x.a[j]-e[i].a[j])*(x.a[j]-e[i].a[j]);
		mac=std::max(mac,sum);
		mic=std::min(mic,sum);
	}
	return mac-mic;
}
void SA(){
	o=as;
	double t=1000.0;
	while(t>1e-14){
		node no;
		for(reg int i=1;i<=n;++i)
			no.a[i]=o.a[i]+(double)((rand()*2)-32767)*t;
		double nw=calc(no);
		double delta=nw-ans;
		if(delta<0){
			ans=nw;
			as=o=no;
		}
		else if(exp(-delta/t)*32767>rand()) o=no;
		t*=0.99997;
	}
}
void work(){
	for(reg int i=1;i<=n;++i)
		as.a[i]=c[i]/(n+1);
	for(reg int i=1;i<=3;++i) SA();
}
int main(){
	srand(9999997);
	scanf("%d",&n);
	for(reg int i=1;i<=n+1;++i){
		for(reg int j=1;j<=n;++j){
			scanf("%lf",&ss);
			e[i].a[j]=ss;
			c[j]+=ss;
		}
	}
	work();
	for(reg int i=1;i<=n;++i)
		printf("%.3lf ",as.a[i]);
}

另外,调参要有耐心。
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值