1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <ctime> #include <utility> using namespace std; typedef pair<double, double> PDD; const int N = 1010; int n; struct Point { double x, y, w; } p[N]; PDD ans; double mn = 1e12; double rand(double l, double r) { return (double)rand() / RAND_MAX * (r - l) + l; } double cal(PDD x) { double res = 0; for (int i = 0; i < n; i++) { double dx = x.first - p[i].x, dy = x.second - p[i].y; res += sqrt(dx * dx + dy * dy) * p[i].w; } if (res < mn) { ans = x; mn = res; } return res; } void SimulateAnneal() { PDD cur(rand(0, 1e4), rand(0, 1e4)); for (double t = 1e4; t > 1e-5; t *= 0.996) { PDD np(rand(cur.first - t, cur.first + t), rand(cur.second - t, cur.second + t)); double delta = cal(np) - cal(cur); if (exp(-delta / t) > rand(0, 1)) cur = np; } } int main() { srand(time(NULL)); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf%lf%lf", &p[i].x, &p[i].y, &p[i].w); while ((double)clock() / CLOCKS_PER_SEC < 0.9) SimulateAnneal(); printf("%.3lf %.3lf", ans.first, ans.second); return 0; }
|