結果

問題 No.421 しろくろチョコレート
ユーザー f1b_maxbl00df1b_maxbl00d
提出日時 2020-06-08 13:47:29
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 3,724 bytes
コンパイル時間 2,002 ms
コンパイル使用メモリ 147,908 KB
実行使用メモリ 5,036 KB
最終ジャッジ日時 2023-10-24 15:01:02
合計ジャッジ時間 3,695 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 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 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 4 ms
4,412 KB
testcase_17 AC 4 ms
4,432 KB
testcase_18 AC 5 ms
4,448 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 3 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 3 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 4 ms
5,036 KB
testcase_32 AC 3 ms
4,348 KB
testcase_33 AC 4 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 4 ms
4,416 KB
testcase_38 AC 5 ms
4,568 KB
testcase_39 AC 3 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 3 ms
4,348 KB
testcase_44 AC 3 ms
4,412 KB
testcase_45 AC 2 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 3 ms
4,348 KB
testcase_48 AC 4 ms
4,716 KB
testcase_49 AC 3 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 4 ms
4,368 KB
testcase_52 AC 3 ms
4,348 KB
testcase_53 AC 3 ms
4,348 KB
testcase_54 AC 3 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 5 ms
4,680 KB
testcase_61 AC 4 ms
4,712 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 2 ms
4,348 KB
testcase_64 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
#include <complex>
#include <cstdio>
#include <list>
#include <bitset>

//< in.txt > out.txt
using namespace std;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
const long long MOD = 998244353;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
//typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;

template< typename flow_t >
struct Dinic {
	const flow_t INF;

	struct edge {
		int to;
		flow_t cap;
		int rev;
		bool isrev;
		int idx;
	};

	vector< vector< edge > > graph;
	vector< int > min_cost, iter;

	Dinic(int V) : INF(numeric_limits< flow_t >::max()), graph(V) {}

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

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

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

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

	void output() {
		for (int i = 0; i < graph.size(); i++) {
			for (auto& e : graph[i]) {
				if (e.isrev) continue;
				auto& rev_e = graph[e.to][e.rev];
				cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
			}
		}
	}
};

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	LL N, M;
	cin >> N >> M;
	vector<vector<bool>> maps;
	maps.resize(M, vector<bool>(N, true));
	LL bl = 0, wh = 0;
	for (LL y = 0; y < N; y++) {
		string S;
		cin >> S;
		for (LL x = 0; x < M; x++) {
			if (S[x] == '.')maps[x][y] = false;
			else {
				if ((x + y) & 1)bl++;
				else wh++;
			}
		}
	}
	Dinic<LL> D(M * N + 2);
	LL vx[4] = { 0,1,0,-1 };
	LL vy[4] = { 1,0,-1,0 };
	for (LL x = 0; x < M; x++) {
		for (LL y = 0; y < N; y++) {
			if ((x + y) & 1)D.add_edge(x + y * M, M * N + 1, 1);
			else D.add_edge(M * N, x + y * M, 1);
			if (!maps[x][y])continue;
			for (LL v = 0; v < 4; v++) {
				LL nx = x + vx[v];
				LL ny = y + vy[v];
				if (nx < 0 || nx >= M || ny < 0 || ny >= N)continue;
				if (!maps[nx][ny])continue;
				if ((x + y) & 1)D.add_edge(nx + ny * M, x + y * M, 1);
				else D.add_edge(x + y * M, nx + ny * M, 1);
			}
		}
	}
	LL flow = D.max_flow(M * N, M * N + 1);
	bl -= flow;
	wh -= flow;
	LL ans = min(bl, wh);
	bl -= ans;
	wh -= ans;
	ans *= 10;
	ans += bl + wh;
	ans += flow * 100;
	cout << ans << "\n";
	return 0;
}
0