求助 这道题SA能过嘛,一直wa
查看原帖
求助 这道题SA能过嘛,一直wa
1026365
LinkLoveZelda楼主2024/9/10 20:22
// https://iai.sh.cn/problem/938
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double f64;
const int N = 22;
ll n, a[N][N];
mt19937 rnd(time(0));
inline ll make_int(ll l, ll r)
{
    auto rnk = uniform_int_distribution<ll>(l, r);
    return rnk(rnd);
}
inline f64 make_real(f64 l, f64 r)
{
    auto rnk = uniform_real_distribution<f64>(l, r);
    return rnk(rnd);
}
ll id[N];
ll ans;
inline ll calc()
{
    ll res = 0;
    for (ll i = 1; i <= n; i++)
    {
        if (i != n)
            res += a[id[i]][id[i + 1]];
        else
            res += a[id[i]][id[1]];
    }
    ans = max(ans, res);
    return res;
}
inline void simulate_anneal()
{
    random_shuffle(id + 1, id + 1 + n);
    for (f64 T = 1e4; T > 1e-4; T *= 0.999)
    {
        ll x = make_int(1, n), y = make_int(1, n);
        ll cur = calc();
        swap(id[x], id[y]);
        ll tmp = calc();
        ll delta = tmp - cur;
        if (exp(delta / T) < make_real(0, 1))
            swap(id[x], id[y]);
    }
}
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cin >> n;
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 1; j <= n; j++)
            cin >> a[i][j];
        id[i] = i;
    }
    for (ll i = 1; i <= 100; i++)
        simulate_anneal();
    cout << ans << endl;
    return 0;
}
2024/9/10 20:22
加载中...