模拟退火91pts求助
查看原帖
模拟退火91pts求助
285617
黑影洞人楼主2022/7/25 18:10
#include<cstdio>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<cmath>
#define N 114514
using namespace std;
int n,a,b;
int ans,bfans,x[N],y[N];
const double eps=1e-13;
int calc(){return (x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]);}
void sa(){
	for(int t=2234;t>eps;t*=0.998){
		int aa=a,bb=b;
		a=rand()%n+1,b=rand()%n+1;
		int now=calc(),del=ans-now;
		if(del<0)ans=now,bfans=ans;
		else if(exp(-(double)del/t)*RAND_MAX<rand())a=aa,b=bb;
	}
}
signed main(){
	srand(time(0));
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d%d",&x[i],&y[i]);
	while((double)clock()<CLOCKS_PER_SEC*0.95)sa();
	printf("%d",ans);
	return 0;
}


2022/7/25 18:10
加载中...