結果
| 問題 |
No.861 ケーキカット
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-08-22 16:09:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 95 ms / 1,000 ms |
| コード長 | 4,017 bytes |
| コンパイル時間 | 2,641 ms |
| コンパイル使用メモリ | 205,500 KB |
| 最終ジャッジ日時 | 2025-01-13 10:00:41 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 |
ソースコード
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 922337203685477580LL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}
struct UnionFind {
vector<int> par; // 親を指すvector,-par[親]は木のサイズ
UnionFind(int n):par(n, -1) {} // uniteで親を埋め込んでいく必要あり
inline void init() {fill(par.begin(), par.end(), -1);}
int root(int x) { // 親をたどる&データの整理
if(par[x] < 0) return x;
return par[x] = root(par[x]);
}
bool unite(int x, int y) { // データの結合
x = root(x);
y = root(y);
if(x == y) return false;
if(par[x] > par[y]) swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
bool same(int x, int y) {return root(x) == root(y);} // 所属判定
int size(int x) {return -par[root(x)];} // 木のサイズ
};
const int N = 5;
const int X = 25;
const bitset<X> around("1111110001100011000111111");
//head
bitset<X> simple[X];
bitset<X> around_[16];
bitset<X> cap[9];
int c[N][N];
ll ans = LINF;
bitset<X> now;
UnionFind uft(X);
int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};
inline bool able(int i, int j) {return i >= 0 and j >= 0 and i < 5 and j < 5;}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
rep(i, 25) {
simple[i] = 1<<i;
}
rep(i, 4) {
around_[i+1] = around_[i]|simple[i];
}
rep(i, 4) {
around_[i+5] = around_[i+4]|simple[i*5+4];
}
rep(i, 4) {
around_[i+9] = around_[i+8]|simple[24-i];
}
rep(i, 3) {
around_[i+13] = around_[i+12]|simple[20-i*5];
}
rep(i, 3) {
cap[i] = simple[i+6];
cap[i+3] = simple[i+11];
cap[i+6] = simple[i+16];
}
rep(i, 5) rep(j, 5) cin >> c[i][j];
rep(i, 16) rep(j, i+!i) {
bitset<X> oo = around_[i]^around_[j];
rep(qwer, 2) {
rep(i, 1<<9) {
now = oo;
rep(j, 9) if(i>>j&1) now |= cap[j];
int w = -1, b = -1;
rep(i, 25) {
if(now.test(i)) b = i;
else w = i;
}
if(w == -1 or b == -1) continue;
ll cnt = 0;
uft.init();
bitset<X> check(0);
queue<P> q;
q.emplace(w/5, w%5);
check.set(w);
while(!q.empty()) {
int i, j;
tie(i, j) = q.front();
q.pop();
cnt += c[i][j];
rep(k, 4) {
int ni = i+di[k];
int nj = j+dj[k];
if(able(ni, nj) and !check.test(ni*5+nj) and !now.test(ni*5+nj)) {
check.set(ni*5+nj);
q.emplace(ni, nj);
uft.unite(w, ni*5+nj);
}
}
}
q.emplace(b/5, b%5);
check.set(b);
while(!q.empty()) {
int i, j;
tie(i, j) = q.front();
q.pop();
cnt -= c[i][j];
rep(k, 4) {
int ni = i+di[k];
int nj = j+dj[k];
if(able(ni, nj) and !check.test(ni*5+nj) and now.test(ni*5+nj)) {
check.set(ni*5+nj);
q.emplace(ni, nj);
uft.unite(b, ni*5+nj);
}
}
}
if(uft.size(b) + uft.size(w) == 25) chmin(ans, abs(cnt));
}
oo ^= around;
}
}
cout << ans << endl;
}