萌新刚学模拟退火,求调模拟退火
  • 板块学术版
  • 楼主Dregen_Yor
  • 当前回复10
  • 已保存回复10
  • 发布时间2022/8/12 16:23
  • 上次更新2023/10/27 15:45:47
查看原帖
萌新刚学模拟退火,求调模拟退火
478585
Dregen_Yor楼主2022/8/12 16:23

RT。一直输出0.

#include <bits/stdc++.h>
#include <time.h>
#define d 0.996
using namespace std;

struct node {
	double x, y, r;
} p[15];

struct em {
	double x, y;
} eme[1010];
int n, m;
double ansx, ansy, R, ans;

double cal(double x, double y) {
	double r = R;
	for (int i = 1; i <= n; i++) {
		double nx = x - p[i].x, ny = y - p[i].y;
		r = min(r, sqrt(nx * nx + ny * ny) - p[i].r);
	}
	double sum = 0;
	for (int i = 1; i <= m; i++) {
		double nx = x - eme[i].x, ny = y - eme[i].y;
		if (sqrt(nx * nx + ny * ny) <= r) {
			sum += 1;
		}
	}
	return sum;
}

void work() {
	double x = ansx, y = ansy;
	double t = 3000.0;
	while (t > 1e-15) {
		double X = x + ((rand() << 1) - RAND_MAX) * t;
		double Y = y + ((rand() << 1) - RAND_MAX) * t;
		double now = (double)cal(X, Y);
		double delta = (double)ans - (double)now;
		if (delta < 0) {
			ans = now;
			ansx = X, ansy = Y;
			x = X, y = Y;
		} else if (exp(-delta / t)*RAND_MAX > rand()) {
			x = X, y = Y;
		}
		t *= d;
	}
}

int main() {
	srand(time(0));
	cin >> n >> m >> R;
	for (int i = 1; i <= n; i++) {
		scanf("%lf%lf%lf", &p[i].x, &p[i].y, &p[i].r);
	}
	for (int i = 1; i <= m; i++) {
		scanf("%lf%lf", &eme[i].x, &eme[i].y);
		ansx += eme[i].x, ansy += eme[i].y;
	}
	ansx /= (double)(m), ansy /= (double)(m);
	work();
	work();
	work();
	work();
	work();
	work();
	work();
	cout << ans;
	return 0;
}

2022/8/12 16:23
加载中...