結果

問題 No.1345 Beautiful BINGO
ユーザー 👑 emthrmemthrm
提出日時 2021-01-16 13:17:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 507 ms / 2,000 ms
コード長 3,715 bytes
コンパイル時間 2,383 ms
コンパイル使用メモリ 206,984 KB
最終ジャッジ日時 2025-01-17 21:51:30
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

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