RT,样例都没过,不知道为啥会出锅
刚学模拟退火,求助错在哪里
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>
#define double long double
#define N 1000
#define L 1
#define T0 3e3
#define eps 1e-15
#define dlt 0.996
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
using namespace std;
int n;
double cx, cy, cw;
struct obj {
obj() {}
obj(int _x, int _y, int _w):
x(_x), y(_y), w(_w) {}
double x, y, w;
} o[N];
double calc(double x, double y) {
double sm = 0;
for (int i=0; i<n; i++)
sm += hypot(x-o[i].x, y-o[i].y) * o[i].w;
return sm;
}
double randf() {
return 1.0 * rand() / RAND_MAX;
}
bool check(double delta, double t) {
return delta > 0 || randf() < exp(delta / t);
}
void SA() {
double t = T0;
while (t > eps) {
for (int i=0; i<L; i++) {
double tx = cx + t * (rand() * 2 - RAND_MAX);
double ty = cy + t * (rand() * 2 - RAND_MAX);
double tw = calc(tx, ty);
if (check(cw - tw, t)) cx = tx, cy = ty, cw = min(cw, tw);
}
t *= dlt;
}
}
int main() {
cin >> n, srand(20210310);
for (int i=0; i<n; i++)
cin >> o[i].x >> o[i].y >> o[i].w, cx += o[i].x, cy += o[i].y;
cx /= n, cy /= n, cw = calc(cx, cy);
while (TIME < 0.5) SA();
printf("%.3lf %.3lf", cx, cy);
return 0;
}
顺便说一下我卡时的问题,如果倒数第四行改成TIME<0.6
就T一堆,只能用0.5(话说我也不知道为啥)