Submission #1229587


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;

class MaxFlow {
	struct edge {
		int to, cap, rev;
		edge(int to_, int cap_, int rev_) : to(to_), cap(cap_), rev(rev_) {};
	};

	int V;
	vector<vector<edge>> G;
	vector<int> level;
	vector<int> iter;

	void BFS(int s) {
		fill(level.begin(), level.end(), -1);
		queue<int> que;
		level[s] = 0;
		que.push(s);
		while (!que.empty()) {
			int v = que.front(); que.pop();
			for (size_t i = 0; i < G[v].size(); i++) {
				edge &e = G[v][i];
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					que.push(e.to);
				}
			}
		}
	}

	int DFS(int v, int t, int f) {
		if (v == t) return f;
		for (int &i = iter[v]; i < (int)G[v].size(); i++) {
			edge &e = G[v][i];
			if (e.cap > 0 && level[v] < level[e.to]) {
				int d = DFS(e.to, t, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					G[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}

public:
	MaxFlow(int _V) : V(_V), G(_V), level(_V), iter(_V) {}
	void add(int from, int to, int cap) {
		G[from].push_back(edge(to, cap, G[to].size()));
		G[to].push_back(edge(from, 0, G[from].size() - 1));
	}
	int Dinic(int s, int t) {
		int flow = 0;
		while (true) {
			BFS(s);
			if (level[t] < 0) return flow;
			fill(iter.begin(), iter.end(), 0);
			int f;
			while ((f = DFS(s, t, INF)) > 0) {
				flow += f;
			}
		}
	}
};

const int dx[] = { 1, 0, -1, 0 };
const int dy[] = { 0, 1, 0, -1 };

int main()
{
	int H, W;
	cin >> H >> W;
	vector<string> S(H);
	for (int i = 0; i < H; i++) {
		cin >> S[i];
	}
	MaxFlow mf(H * W * 2 + 2);
	for (int i = 0; i < H * W; i++) {
		if (S[i % H][i / H] == '.') {
			mf.add(i, i + H * W, 1);
		}
		else {
			mf.add(i, i + H * W, INF);
			mf.add(i + H * W, H * W * 2 + 1, INF);
		}
		if (i % H == 0 || i % H == H - 1 || i / H == 0 || i / H == W - 1) {
			mf.add(H * W * 2, i, INF);
		}
	}
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			for (int k = 0; k < 4; k++) {
				int tx = i + dx[k], ty = j + dy[k];
				if (tx >= 0 && tx < H && ty >= 0 && ty < W) {
					mf.add(j * H + i + H * W, ty * H + tx, INF);
				}
			}
		}
	}
	int res = mf.Dinic(H * W * 2, H * W * 2 + 1);
	cout << (res < INF ? res : -1) << endl;
	return 0;
}

Submission Info

Submission Time
Task E - Fences
User kazuma
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2347 Byte
Status WA
Exec Time 51 ms
Memory 3328 KB

Judge Result

Set Name All
Score / Max Score 0 / 150
Status
AC × 18
WA × 1
Set Name Test Cases
All 00_sample.txt, 01_sample.txt, 02_sample.txt, 10_rand_00.txt, 10_rand_01.txt, 10_rand_02.txt, 10_rand_03.txt, 10_rand_04.txt, 10_rand_05.txt, 10_rand_06.txt, 10_rand_07.txt, 10_rand_08.txt, 11_hashi_00.txt, 11_hashi_01.txt, 11_hashi_02.txt, 12_rect_00.txt, 12_rect_01.txt, 12_rect_02.txt, 99_all_one.txt
Case Name Status Exec Time Memory
00_sample.txt AC 1 ms 256 KB
01_sample.txt AC 1 ms 256 KB
02_sample.txt AC 1 ms 256 KB
10_rand_00.txt AC 1 ms 256 KB
10_rand_01.txt AC 2 ms 384 KB
10_rand_02.txt AC 11 ms 1152 KB
10_rand_03.txt AC 14 ms 1152 KB
10_rand_04.txt AC 8 ms 2816 KB
10_rand_05.txt AC 12 ms 2944 KB
10_rand_06.txt AC 8 ms 1152 KB
10_rand_07.txt AC 11 ms 1920 KB
10_rand_08.txt AC 20 ms 2688 KB
11_hashi_00.txt AC 16 ms 1536 KB
11_hashi_01.txt AC 5 ms 768 KB
11_hashi_02.txt AC 6 ms 1664 KB
12_rect_00.txt AC 51 ms 2560 KB
12_rect_01.txt AC 8 ms 896 KB
12_rect_02.txt AC 9 ms 2304 KB
99_all_one.txt WA 8 ms 3328 KB