版本比较
[杂项] 模拟退火
正文
增加 57 行删除 1 行
14 行未改动1516171819192021222348 行未改动7273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013114 行未改动
:::
Update on 2025.10.21 : Update on 2025.10.21 :说的可太对了,退火就是个玄学算法,正确公式过不了,改一改就又能过了
# 算法过程
48 行未改动 } ansx/=n;ansy/=n;answ=calc(ansx,ansy); nowx=ansx;nowy=ansy;```
### 代码实现
[8.65s 1000KB](https://www.luogu.com.cn/record/239242430)
```cpp#include <bits/stdc++.h>using namespace std;int n;double nowx,nowy,answ,ansx,ansy;double t0=1e-15,eps=1e-30,q=0.998;struct objects{ double x,y,w;}ob[1010];double calc(double x,double y) { double ans=0; for(int i=1;i<=n;i++) { ans=ans+sqrt((x-ob[i].x)*(x-ob[i].x)+(y-ob[i].y)*(y-ob[i].y))*ob[i].w; } return ans;}void simulated_annealling(){ //模拟退火 double t=3000; for(;t-t0>eps;t*=q) { double tx=nowx+((rand()<<1)-RAND_MAX)*t; double ty=nowy+((rand()<<1)-RAND_MAX)*t; double tw=calc(tx,ty); double delta=tw-answ; if(delta<eps) { ansx=tx;ansy=ty;answ=tw; nowx=ansx;nowy=ansy; } else if(exp(-delta/t)*RAND_MAX>rand()){ nowx=tx;nowy=ty; } }}int main(){ // freopen("test.in","r",stdin); // freopen("test.out","w",stdout); srand(time(0)); cin>>n; for(int i=1;i<=n;i++) { cin>>ob[i].x>>ob[i].y>>ob[i].w; ansx+=ob[i].x; ansy+=ob[i].y; } ansx/=n;ansy/=n;answ=calc(ansx,ansy); nowx=ansx;nowy=ansy; while((double)clock()/CLOCKS_PER_SEC<=0.95) { simulated_annealling(); } printf("%.3lf %.3lf\n",ansx,ansy); return 0;}```