模拟退火,求查错
查看原帖
模拟退火,求查错
246800
wocaicai楼主2020/10/25 17:48

rt ,一道简单的退火(目前实际上我写的是爬山)

应该是算法正确性的问题qwq

#include<bits/stdc++.h>
using namespace std ;
#define int long long
#define kkksc signed main
#define ddd double
#define maxn 1010
#define lc (o << 1)
#define rc ((o << 1) | 1 )
#define mem(x , a) memset(x , a , sizeof(x))
int read(){
	int ans = 0 , f = 1 ; char ch = getchar() ;
	while(ch < '0' || ch > '9'){ if(ch == '-') f = -1 ; ch = getchar() ; }
	while(ch >= '0' && ch <= '9') {
		ans = (ans * 10) + ch - '0' ;
		ch = getchar() ;
	}
	return ans * f ;
}
int n , m ;
ddd r , totx , toty ; 
struct node{
	ddd x , y , r ; 
}no[maxn];
ddd xi[maxn] , yi[maxn] ; 
ddd dis(ddd x1 , ddd x2 , ddd y1 , ddd y2){
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) ; 
}
int calc(ddd x , ddd y){
	int sum = 0 ; 
	ddd ddis , R = r ; 
//		printf("R : %lf\n" , R) ;  
	for(int i = 1 ; i <= n ; i++){
		ddis = dis(x , no[i].x , y , no[i].y ) ; 
//		printf("ddis : %lf no[].x : %lf y : %lf " , ddis , x , y ) ; 
		ddis -= no[i].r ; 
		R = min(R , ddis) ; 
	}
//	printf("R : %lf\n" , R) ;  
	for(int i = 1 ; i <= m ; i++){
		ddis = dis(x , y , xi[i] , yi[i]) ; 
		if(ddis <= R) sum++ ; 
	}
	return sum ; 
}
ddd eps = 1e-15 ; 
#define maxx RAND_MAX 
ddd ansx , ansy ; 
int answ , tot; 
void sa(){
	ddd T = 2000 ; 
	tot = calc(ansx , ansy) ; 
	while(T > eps){
		ddd tx = ansx + (rand() * 2 - maxx) * T ; 
		ddd ty = ansy + (rand() * 2 - maxx) * T ; 
		int temp = calc(tx , ty) ; 
//		printf("tx : %lf ty : %lf temp :%lld \n" , tx , ty , temp) ; 
		if(temp > tot){
			tot = temp ; answ = max(answ , tot) ; 
			ansx = tx ; ansy = ty ; 
		}
//		else if(exp((answ - temp) / T) * maxx < rand())
//			tot = temp , ansx = tx , ansy = ty ; 
		T *= 0.98 ; 
	}
}
kkksc(){
	srand(time(0)) ; 
	n = read() , m = read() ; r = read() ; 
	for(int i = 1 ; i <= n ; i++)
		no[i].x = read() , no[i].y = read() , no[i].r = read() ; 
	for(int i = 1 ; i <= m ; i++)
		xi[i] = read() , yi[i] = read() , totx += xi[i] , toty += yi[i]; 
	ansx = totx/(ddd)m , ansy = toty/(ddd)m ; 
	answ = calc(ansx , ansy) ; 
	for(int i = 1 ; i <= 10 ; i++)
		sa() ; 
	printf("%lld\n" , answ) ; 
}

2020/10/25 17:48
加载中...