結果

問題 No.421 しろくろチョコレート
ユーザー 👑 jupirojupiro
提出日時 2020-07-10 15:17:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 3,290 bytes
コンパイル時間 1,555 ms
コンパイル使用メモリ 144,244 KB
実行使用メモリ 4,436 KB
最終ジャッジ日時 2023-10-24 15:01:26
合計ジャッジ時間 3,046 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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>
#include <numeric>
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; }
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

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

struct Dinic {
	struct edge {
		int to;
		ll cap;
		int rev;
		bool isrev;
		int idx;
		edge(int _to, ll _cap, int _rev, bool _isrev, int _idx) :to(_to), cap(_cap), rev(_rev), isrev(_isrev), idx(_idx) {}
	};
	vector<vector<edge>> g;
	vector<int> min_cost, iter;
	Dinic(int n) :g(n) {}

	void add_edge(int from, int to, ll cap, int idx = -1) {
		g[from].emplace_back(to, cap, (int)g[to].size(), false, idx);
		g[to].emplace_back(from, 0, (int)g[from].size() - 1, true, idx);
	}

	bool bfs(int s, int t) {
		min_cost.assign(g.size(), -1);
		queue<int> q;
		q.emplace(s);
		min_cost[s] = 0;
		while (!q.empty() && min_cost[t] == -1) {
			int cur = q.front();
			q.pop();
			for (auto& e : g[cur]) {
				if (e.cap > 0 && min_cost[e.to] == -1) {
					min_cost[e.to] = min_cost[cur] + 1;
					q.push(e.to);
				}
			}
		}
		return min_cost[t] != -1;
	}

	ll dfs(int idx, const int t, ll flow) {
		if (idx == t) return flow;
		for (int& i = iter[idx]; i < g[idx].size(); i++) {
			edge& e = g[idx][i];
			if (e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
				ll d = dfs(e.to, t, min(flow, e.cap));
				if (d > 0) {
					e.cap -= d;
					g[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}

	ll max_flow(int s, int t) {
		ll flow = 0;
		while (bfs(s, t)) {
			iter.assign(g.size(), 0);
			ll f = 0;
			while ((f = dfs(s, t, INF)) > 0) flow += f;
		}
		return flow;
	}
};


int dh[] = { 1,-1,0,0 };
int dw[] = { 0,0,1,-1 };

int n, m;

int cnv(int i, int j) {
	return i * m + j;
}
int main()
{
	
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	

	cin >> n >> m;
	vector<string> vs(n); for (int i = 0; i < n; i++) cin >> vs[i];
	int b = 0, w = 0;
	Dinic g(n * m + 2);
	int s = n * m;
	int t = n * m + 1;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (vs[i][j] == 'b') {
				b++;
				g.add_edge(s, cnv(i, j), 1);
				for (int k = 0; k < 4; k++) {
					int nh = i + dh[k];
					int nw = j + dw[k];
					if (nh < 0 or nh >= n or nw < 0 or nw >= m) continue;
					if (vs[nh][nw] == 'w') g.add_edge(cnv(i, j), cnv(nh, nw), 1);
				}
			}
			if (vs[i][j] == 'w') {
				w++;
				g.add_edge(cnv(i, j), t, 1);
			}
		}
	}
	int mx = g.max_flow(s, t);
	b -= mx;
	w -= mx;
	int mn = min(b, w);
	b -= mn;
	w -= mn;
	int res = mx * 100 + mn * 10 + b + w;
	cout << res << endl;
	return 0;
}
0