0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 13:16:26 +00:00

P1337 [JSOI2004] 平衡点 / 吊打XXX

https://www.luogu.com.cn/record/87691240
This commit is contained in:
Baoshuo Ren 2022-09-27 08:18:06 +08:00
parent 6e6213f281
commit 1cdfcd8653
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

82
Luogu/P1337/P1337.cpp Normal file
View File

@ -0,0 +1,82 @@
#include <iostream>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <random>
#include <tuple>
#include <utility>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
std::vector<std::tuple<int, int, int>> a(n);
for (auto &o : a) {
cin >> std::get<0>(o) >> std::get<1>(o) >> std::get<2>(o);
}
std::mt19937 gen(std::chrono::system_clock::now().time_since_epoch().count());
auto random = [&](int low, int high) -> int {
std::uniform_int_distribution<> dist(low, high);
return dist(gen);
};
auto potential_energy = [&](double x, double y) -> double {
double res = 0;
for (auto &o : a) {
double xx = x - std::get<0>(o),
yy = y - std::get<1>(o);
res += std::sqrt(std::pow(xx, 2) + std::pow(yy, 2)) * std::get<2>(o);
}
return res;
};
double ans_x = 0,
ans_y = 0,
ans = 1e18;
for (int i = 1; i <= 10; i++) {
double xx = ans_x,
yy = ans_y;
double t = 21000;
while (t > 1e-15) {
double tmp_x = ans_x + random(-100000, 100000) * t,
tmp_y = ans_y + random(-100000, 100000) * t;
double tmp_ans = potential_energy(tmp_x, tmp_y);
double delta = tmp_ans - ans;
if (delta < 0) {
ans_x = xx = tmp_x;
ans_y = yy = tmp_y;
ans = tmp_ans;
} else if (std::exp(-delta / t) * 10000 > random(0, 10000)) {
xx = tmp_x;
yy = tmp_y;
}
t *= 0.998;
}
}
cout << std::fixed << std::setprecision(3) << ans_x << ' '
<< std::fixed << std::setprecision(3) << ans_y << endl;
return 0;
}