結果

問題 No.1345 Beautiful BINGO
ユーザー 👑 emthrmemthrm
提出日時 2021-01-16 13:17:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 446 ms / 2,000 ms
コード長 3,715 bytes
コンパイル時間 2,175 ms
コンパイル使用メモリ 212,472 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 23:50:04
合計ジャッジ時間 11,913 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 20 ms
4,376 KB
testcase_23 AC 434 ms
4,380 KB
testcase_24 AC 190 ms
4,380 KB
testcase_25 AC 10 ms
4,376 KB
testcase_26 AC 88 ms
4,376 KB
testcase_27 AC 429 ms
4,376 KB
testcase_28 AC 9 ms
4,376 KB
testcase_29 AC 88 ms
4,380 KB
testcase_30 AC 40 ms
4,376 KB
testcase_31 AC 20 ms
4,380 KB
testcase_32 AC 3 ms
4,376 KB
testcase_33 AC 436 ms
4,376 KB
testcase_34 AC 5 ms
4,376 KB
testcase_35 AC 5 ms
4,376 KB
testcase_36 AC 431 ms
4,380 KB
testcase_37 AC 5 ms
4,376 KB
testcase_38 AC 90 ms
4,380 KB
testcase_39 AC 438 ms
4,376 KB
testcase_40 AC 200 ms
4,376 KB
testcase_41 AC 199 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 1 ms
4,380 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 1 ms
4,376 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 1 ms
4,380 KB
testcase_52 AC 435 ms
4,376 KB
testcase_53 AC 438 ms
4,380 KB
testcase_54 AC 446 ms
4,376 KB
testcase_55 AC 445 ms
4,376 KB
testcase_56 AC 439 ms
4,380 KB
testcase_57 AC 434 ms
4,376 KB
testcase_58 AC 443 ms
4,380 KB
testcase_59 AC 434 ms
4,376 KB
testcase_60 AC 444 ms
4,384 KB
testcase_61 AC 432 ms
4,376 KB
testcase_62 AC 429 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T>
struct WarshallFloyd {
  std::vector<std::vector<T>> graph, dist;

  WarshallFloyd(const std::vector<std::vector<T>> &graph, const T TINF) : graph(graph), dist(graph), TINF(TINF) {
    n = graph.size();
    internal.assign(n, std::vector<int>(n, -1));
    for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
      if (dist[i][j] > dist[i][k] + dist[k][j]) {
        dist[i][j] = dist[i][k] + dist[k][j];
        internal[i][j] = k;
      }
    }
  }

  void add(int src, int dst, T cost) {
    srcs.emplace_back(src);
    dsts.emplace_back(dst);
    costs.emplace_back(cost);
  }

  void calc() {
    std::set<int> vers;
    int sz = srcs.size();
    for (int i = 0; i < sz; ++i) {
      int s = srcs[i], t = dsts[i];
      T cost = costs[i];
      if (cost < graph[s][t]) graph[s][t] = cost;
      if (dist[s][t] >= cost) {
        dist[s][t] = cost;
        internal[s][t] = -1;
      }
      vers.emplace(s);
      vers.emplace(t);
    }
    for (int v : vers) {
      for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
        if (dist[i][j] > dist[i][v] + dist[v][j]) {
          dist[i][j] = dist[i][v] + dist[v][j];
          internal[i][j] = v;
        }
      }
    }
    srcs.clear();
    dsts.clear();
    costs.clear();
  }

  bool has_negative_cycle() const {
    for (int i = 0; i < n; ++i) {
      if (dist[i][i] < 0) return true;
    }
    return false;
  }

  std::vector<int> build_path(int s, int t) const {
    std::vector<int> res;
    if (dist[s][t] != TINF) {
      build_path(s, t, res);
      res.emplace_back(t);
    }
    return res;
  }

private:
  const T TINF;
  int n;
  std::vector<std::vector<int>> internal;
  std::vector<int> srcs, dsts;
  std::vector<T> costs;

  void build_path(int s, int t, std::vector<int> &path) const {
    int k = internal[s][t];
    if (k == -1) {
      path.emplace_back(s);
    } else {
      build_path(s, k, path);
      build_path(k, t, path);
    }
  }
};

int main() {
  int n, m; cin >> n >> m;
  vector<vector<int>> a(n, vector<int>(n)); REP(i, n) REP(j, n) cin >> a[i][j];
  int ans = INF;
  REP(x, 2) REP(y, 2) REP(z, 1 << n) {
    vector mag(n, vector(n, false));
    if (x) {
      REP(i, n) mag[i][i] = true;
    }
    if (y) {
      REP(i, n) mag[i][n - 1 - i] = true;
    }
    REP(i, n) {
      if (z >> i & 1) fill(ALL(mag[i]), true);
    }
    vector<int> need(n, 0);
    int cur = 0;
    REP(i, n) REP(j, n) {
      if (mag[i][j]) {
        cur += a[i][j];
      } else {
        need[j] += a[i][j];
      }
    }
    sort(ALL(need), greater<int>());
    int row = x + y + __builtin_popcount(z);
    if (row + n >= m) {
      REP(_, m - row) {
        cur += need.back();
        need.pop_back();
      }
      chmin(ans, cur);
    }
  }
  cout << ans << '\n';
  return 0;
}
0