为什么求势能啊,萌新不懂QAQ
查看原帖
为什么求势能啊,萌新不懂QAQ
160087
悬浮的气球楼主2022/11/24 21:43
#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<double, double> PDD;
const int N = 1e3 + 10;

int n;
PDD P[N];
double w[N];
PDD ans;
double minweight = 1e8;

double rand(double l, double r)
{
    return (double)rand() / RAND_MAX * (r - l) + l;
}

double get_dist(double x, double y)
{
    return sqrt(x * x + y * y);
}

double weight(PDD np)
{
    double sx = 0, sy = 0;
    for (int i = 0; i < n; ++ i)
    {
        double dx = P[i].x - np.x;
        double dy = P[i].y - np.y;
        double len = get_dist(dx, dy);
        if (len != 0) sx += dx / len * w[i] , sy += dy /len * w[i];
    }

    double dist = get_dist(sx, sy);
    if (dist < minweight)
        minweight = dist, ans = np;

    return dist;
}

void Simulate_Anneal()
{
    PDD cur(rand(-10000, 10000), rand(-10000, 10000));

    for (double t = 1e4; t > 1e-4; t *= 0.99)
    {
        PDD np(rand(cur.x - t, cur.x + t), rand(cur.y - t, cur.y + t));
        double delta = weight(np) - weight(cur);
        if (exp(-delta / t) > (double)rand() / RAND_MAX) 
            swap(cur, np);
    }
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++ i)
        scanf("%lf%lf%lf", &P[i].x, &P[i].y, &w[i]);

    while ((double)clock() / CLOCKS_PER_SEC < 0.95)
        Simulate_Anneal();

    printf("%.3lf %.3lf\n", ans.x, ans.y);

    return 0;
}
2022/11/24 21:43
加载中...