QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#370932#6394. Turn on the Lightricofx#TL 0ms0kbC++171.8kb2024-03-29 19:43:212024-03-29 19:43:23

Judging History

你现在查看的是最新测评结果

  • [2024-03-29 19:43:23]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-03-29 19:43:21]
  • 提交

answer

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;

#define fi first
#define se second
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
//std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());

const ll mod = 998244353;
const int N = 4e6 + 5;
const ll inf = 1e15;
void add(int &x, int y) {
    ((x += y) >= mod) && (x -= mod);
}
int n;
ll w[25], dis[25][25], dp[(1 << 22) + 5];

void MAIN() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) dis[i][j] = inf;
    }
    for (int i = 0; i < n; i++) cin >> w[i];
    for (int i = 0; i < n; i++) {
        dis[i][i] = 0;
        for (int j = 0; j < n; j++) dis[i][(i + j) % n] = min(dis[i][(i + j) % n], w[j]);
    }
    for (int k = 0; k < n; k++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
        }
    }
 //   cerr << dis[1][2] << '\n';
    int mask = ((1 << n) - 1);
    for (int i = 0; i <= mask; i++) dp[i] = inf;
    dp[0] = 0;
    for (int s = 0; s <= mask; s++) {
       // cout << s << ' ' << dp[s] << '\n';
        for (int j = 0; j < n; j++) if (j == 0 || ((s >> j) & 1)) {
            for (int k = 0; k < n; k++) dp[s | (1 << k)] = min(dp[s | (1 << k)], dp[s] + dis[k][j]);
        }
    }
    for (int s = mask; s >= 0; s--) {
        for (int k = 0; k < n; k++) if (!(s >> k) & 1) dp[s] = min(dp[s], dp[s | (1 << k)]);
    }
    ll ans = 0;
    for (int i = 1; i <= mask; i++) ans = (ans + dp[i] * (ll)i % mod) % mod;
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int T = 1;
    //cin >> T;
    while (T--) MAIN();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

3

output:


result: