結果

問題 No.1345 Beautiful BINGO
ユーザー zeronosu77108
提出日時 2021-01-16 15:09:00
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,141 bytes
コンパイル時間 4,520 ms
コンパイル使用メモリ 143,364 KB
実行使用メモリ 14,372 KB
最終ジャッジ日時 2024-11-27 17:31:12
合計ジャッジ時間 41,059 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 29 WA * 22 TLE * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

//
// Created by zeronosu77108 on Jan 15, 2021.
//
#include <iostream>
#include <iomanip>
#include <vector>
#include <utility>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <queue>
#include <cmath>
#include <numeric>
#include <set>
#include <complex>
#include <optional>
#include <climits>

using namespace std;
struct aaa{aaa(){cin.tie(nullptr); ios::sync_with_stdio(false); cout<<fixed<<setprecision(20);};}aaa;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v){o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
#define debug(v) {cerr<<"\033[1;36m[debug]\033[m "<<#v<<" : "<<(v)<<endl;}

int main() {
    int n, m;
    cin >> n >> m;

    vector a(n, vector(n, 0));
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) cin >> a[i][j];
    }

    vector row(n, 0);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            row[i] += a[i][j];
        }
    }

    vector col(n, vector(1<<n, 0));
    for (int i=0; i<1<<n; i++) {
        for (int j=0; j<n; j++) {
            for (int k=0; k<n; k++) {
                if ((i>>k&1)==0) col[j][i] += a[k][j];
            }
        }
    }

    long comb = (1L<<m) - 1;
    long ans = LONG_MAX;
    while (comb < 1L << (2*n+1)) {
        long x=comb&-comb, y=comb+x;
        comb = ((comb & ~y) / x >> 1) | y;

        long d = comb & 3;
        long r = ((comb)>>2) & ((1<<n)-1);
        long c = ((comb)>>(n+2));


        long sum = 0;
        for (int i=0; i<n; i++) {
            if (r>>i&1) {
                sum += row[i];
            }
        }

        for (int i=0; i<n; i++) {
            if (c>>i&1) {
                sum += col[i][r];
            }
        }

        if (d&1) {
            for (int i=0; i<n; i++) {
                if (r>>i&1 || c>>i&1) continue;
                sum += a[i][i];
            }
        }

        if (d&2) {
            for (int i=0; i<n; i++) {
                if (r>>i&1 || c>>(n-i-1)&1) continue;
                sum += a[i][n-i-1];
            }
        }

        ans = min(ans, sum);
    }

    cout << ans << endl;
}
0