結果

問題 No.861 ケーキカット
ユーザー 👑 jupirojupiro
提出日時 2020-01-26 18:48:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,781 bytes
コンパイル時間 1,288 ms
コンパイル使用メモリ 122,892 KB
実行使用メモリ 7,512 KB
最終ジャッジ日時 2023-10-12 12:20:07
合計ジャッジ時間 30,432 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

class UnionFind {
public:
	vector<ll> Parent;
	UnionFind(ll N) {
		Parent = vector<ll>(N, -1);
	}
	ll root(ll A) {
		if (Parent[A] < 0) {
			return A;
		}
		return Parent[A] = root(Parent[A]);
	}

	ll size(ll A) {
		return -Parent[root(A)];
	}

	bool connect(ll A, ll B) {
		A = root(A);
		B = root(B);
		if (A == B) {
			return false;
		}
		if (size(A) < size(B)) {
			swap(A, B);
		}

		Parent[A] += Parent[B];
		Parent[B] = A;

		return true;
	}

};
ll c[5][5];
ll cnv(ll h, ll w) {
	return h * 5 + w;
}
ll res = INF;
ll all;
vector<bool> visited;
void dfs(ll S) {
	visited[S] = true;
	{
		UnionFind uni(25);
		ll tmp = 0;
		for (ll i = 0; i < 5; i++) {
			for (ll j = 0; j < 5; j++) {
				bool flag = (S >> cnv(i, j) & 1);
				if (flag) tmp += c[i][j];
				if (i > 0 && (flag == (S >> cnv(i - 1, j) & 1))) uni.connect(cnv(i, j), cnv(i - 1, j));
				if (i < 4 && (flag == (S >> cnv(i + 1, j) & 1))) uni.connect(cnv(i, j), cnv(i + 1, j));
				if (j > 0 && (flag == (S >> cnv(i, j - 1) & 1))) uni.connect(cnv(i, j), cnv(i, j - 1));
				if (j < 4 && (flag == (S >> cnv(i, j + 1) & 1))) uni.connect(cnv(i, j), cnv(i, j + 1));
			}
		}
		set<ll> s; for (ll i = 0; i < 25; i++) s.insert(uni.root(i));
		if(s.size() <= 2) chmin(res, llabs(tmp - (all - tmp)));
	}
	for (ll i = 0; i < 5; i++) for (ll j = 0; j < 5; j++) if (!(S >> cnv(i, j) & 1)) {
		bool flag = false;
		if (i > 0 && (S >> cnv(i - 1, j)) & 1) flag = true;
		if (i < 4 && (S >> cnv(i + 1, j)) & 1) flag = true;
		if (j > 0 && (S >> cnv(i, j - 1) & 1)) flag = true;
		if (j < 4 && (S >> cnv(i, j + 1) & 1)) flag = true;
		ll ns = S + (1LL << cnv(i, j));
		if (flag && !visited[ns]) dfs(S + (1LL << cnv(i, j)));
	}
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	for (ll i = 0; i < 5; i++) for (ll j = 0; j < 5; j++) {
		scanf("%lld", &c[i][j]); all += c[i][j];
	}

	visited = vector<bool>(1LL << 25, false);
	dfs(1);

	cout << res << endl;
	return 0;
}
0