QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#370932 | #6394. Turn on the Light | ricofx# | TL | 0ms | 0kb | C++17 | 1.8kb | 2024-03-29 19:43:21 | 2024-03-29 19:43:23 |
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;
}
詳細信息
Test #1:
score: 0
Time Limit Exceeded
input:
3