AcWing 3167. 贴一个模拟退火,简洁的代码
原题链接
中等
作者:
静态规划
,
2021-02-02 19:16:26
,
所有人可见
,
阅读 665
#include <iostream>
#include <cmath>
using namespace std;
const int s = 200;
const double eps = 1e-6;
int dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0};
int xx[s], yy[s];
int n;
double dis(double x, double y) {
double sum = 0;
for (int i = 0; i < n; i++) {
double a = xx[i]-x, b = y-yy[i];
sum +=sqrt(a*a+b*b);
}
return sum;
}
int main () {
cin >> n;
for (int i = 0; i < n; i++) cin >> xx[i] >> yy[i];
double t = 1, ans = 1e9, x = xx[0], y = yy[0];
while (t > eps) {
int fl =1;
for (int i = 0; i < 4; i++) {
double nx = x + dx[i] * t, ny = y + dy[i] * t;
double v = dis(nx, ny);
if (v < ans) {
ans = v;
x = nx, y = ny;
fl = 0;
}
}
if(fl) t*=0.98;
}
printf("%.0f", ans);
return 0;
}
大佬,这还是模拟退火吗?为啥没有用到随机数鸭?