結果
問題 |
No.1345 Beautiful BINGO
|
ユーザー |
![]() |
提出日時 | 2021-01-16 14:08:55 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,928 bytes |
コンパイル時間 | 2,094 ms |
コンパイル使用メモリ | 206,124 KB |
最終ジャッジ日時 | 2025-01-17 22:36:39 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 34 TLE * 27 |
ソースコード
// #include <atcoder/all> // using namespace atcoder; // using mint = modint1000000007; // using mint = modint998244353; #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define rrep(i, n) for (int i = (int)n-1; i >= 0; --i) using namespace std; using ll = long long; template<typename T> inline bool chmax(T& a, const T& b) { if (a < b){ a = b; return true; } return false; } template<typename T> inline bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } return false; } /** * @brief 多次元 vector の作成 * @author えびちゃん */ namespace detail { template<typename T, int N> auto make_vec(vector<int>& sizes, T const& x) { if constexpr (N == 1) { return vector(sizes[0], x); } else { int size = sizes[N-1]; sizes.pop_back(); return vector(size, make_vec<T, N-1>(sizes, x)); } } } template<typename T, int N> auto make_vec(int const(&sizes)[N], T const& x = T()) { vector<int> s(N); for (int i = 0; i < N; ++i) s[i] = sizes[N-i-1]; return detail::make_vec<T, N>(s, x); } __attribute__((constructor)) void fast_io() { ios::sync_with_stdio(false); cin.tie(nullptr); } int main() { int n, m; cin >> n >> m; auto a = make_vec<int>({n, n}); rep(i, n) rep(j, n) cin >> a[i][j]; auto dp = make_vec<ll>({1 << (n + 2), n + 2}, LLONG_MAX); rep(bit, 1 << n) { ll now = 0; rep(j, n) { if (bit >> j & 1) now += a[0][j]; } chmin(dp[bit | ((bit & 1) << n) | (bit >> (n - 1) & 1) << (n + 1)][bit == (1 << n) - 1], now); } for (int i = 1; i < n; ++i) { auto ndp = make_vec<ll>({1 << (n + 2), n + 2}, LLONG_MAX); for (int bit = 0; bit < 1 << (n + 2); ++bit) { for (int x = 0; x <= n; ++x) { if (dp[bit][x] == LLONG_MAX) continue; for (int nbit = bit; nbit >= 0; --nbit) { nbit &= bit; ll now = 0; vector<bool> used(n); rep(j, n) if (nbit >> j & 1) now += a[i][j], used[j] = true; if ((nbit >> n & 1) && !used[i]) now += a[i][i], used[i] = true; if ((nbit >> (n + 1) & 1) && !used[n-1-i]) now += a[i][n-1-i], used[n-1-i] = true; int nx = x + all_of(used.begin(), used.end(), [](bool b) { return b; }); chmin(ndp[nbit][nx], dp[bit][x] + now); } ll now = 0; rep(j, n) now += a[i][j]; chmin(ndp[bit][x + 1], dp[bit][x] + now); } } dp = move(ndp); } ll ans = LLONG_MAX; rep(bit, 1 << (n + 2)) rep(x, n + 1) { int y = x + __builtin_popcount(bit); if (y >= m) chmin(ans, dp[bit][x]); } cout << ans << '\n'; }