學習博客
題目鏈接
玄學算法。。。一開始不知道調參wa到懷疑人生
後來嘗試改一下delta竟然過了。。。
這道題正解可以用爬山算法來做,佔坑有空補一下
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define ft first
#define sd second
#define all(c) ((c).begin()), ((c).end())
#define mp(a, b) make_pair(a, b)
#define pb(x) push_back(x)
const int maxn = 1e5+5;
typedef long long ll;
const ll mod = 1e9+7;
int Case = 1;
int n, m;
struct node{
int x, y, z;
}cc[maxn];
int sx, sy;
double resx, resy;
double res = 1e18, t;
const double delta = 0.992;//記得調參
double cal_energy(double x, double y) {//計算能量
double rt = 0;
for(int i = 1; i <= n; i++) {
double deltax = x-cc[i].x, deltay = y-cc[i].y;
rt += sqrt(deltax*deltax+deltay*deltay)*cc[i].z;
}
return rt;
}
void simulate_anneal() {//模擬退火
double x = resx, y = resy;
t = 2000;
while(t > 1e-14) {
double X = x+((rand()<<1)-RAND_MAX)*t;
double Y = y+((rand()<<1)-RAND_MAX)*t;
double now = cal_energy(X, Y);
double new_delta = now-res;
if(new_delta < 0) {
x = X;y = Y;
resx = x;resy = y;res = now;
}
else if(exp(-new_delta/t)*RAND_MAX>rand())x = X, y = Y;
t *= delta;
}
}
void solve() {
cin>>n;
for(int i = 1; i <= n; i++)
cin>>cc[i].x>>cc[i].y>>cc[i].z, sx+=cc[i].x, sy+=cc[i].y;
resx = 1.0*sx/n;resy = 1.0*sy/n;
//多調用幾次更精確。。
simulate_anneal();
simulate_anneal();
simulate_anneal();
printf("%.3f %.3f\n", resx, resy);
return;
}
int main() {
srand(rand());srand(rand());srand(rand());
ios::sync_with_stdio(false);cin.tie(0);std::cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif
//scanf("%d", &Case);
while(Case--){
solve();
}
return 0;
}