Submission #908545


Source Code Expand

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

#define each(it,o) for(auto it = (o).begin(); it != (o).end(); ++ it)

struct MaximumFlow {
	typedef int Index;
	typedef int Flow;
	static const Flow InfCapacity = INF;
	struct Edge {
		Index to;
		Flow capacity;
		Index rev;
	};
	vector<vector<Edge> > g;
	void init(Index n) { g.assign(n, vector<Edge>()); }
	void add(Index i, Index j, Flow capacity) {
		Edge e, f; e.to = j, f.to = i; e.capacity = capacity, f.capacity = 0;
		g[i].push_back(e); g[j].push_back(f);
		g[i].back().rev = (Index)g[j].size() - 1; g[j].back().rev = (Index)g[i].size() - 1;
	}
	void addB(Index i, Index j, Flow capacity) {
		Edge e, f; e.to = j, f.to = i; e.capacity = capacity, f.capacity = capacity;
		g[i].push_back(e); g[j].push_back(f);
		g[i].back().rev = (Index)g[j].size() - 1; g[j].back().rev = (Index)g[i].size() - 1;
	}
	//gを破壊する
	Flow maximumFlow(int s, int t) {
		int n = g.size();
		vector<Index> level(n);
		Flow total = 0; bool update;
		do {
			update = false;
			fill(level.begin(), level.end(), -1); level[s] = 0;
			queue<Index> q; q.push(s);
			for(Index d = n; !q.empty() && level[q.front()] < d; ) {
				int u = q.front(); q.pop();
				if(u == t) d = level[u];
				each(e, g[u]) if(e->capacity > 0 && level[e->to] == -1)
					q.push(e->to), level[e->to] = level[u] + 1;
			}
			vector<Index> iter(n);
			for(Index i = 0; i < n; i ++) iter[i] = (int)g[i].size() - 1;
			while(1) {
				Flow f = augment(level, iter, s, t, InfCapacity);
				if(f == 0) break;
				total += f; update = true;
			}
		} while(update);
		return total;
	}
	Flow augment(vector<Index> &level, vector<Index> &iter, Index u, Index t, Flow f) {
		if(u == t || f == 0) return f;
		Index lv = level[u];
		if(lv == -1) return 0;
		level[u] = -1;
		for(; iter[u] >= 0; -- iter[u]) {
			Edge &e = g[u][iter[u]];
			if(level[e.to] <= lv) continue;
			Flow l = augment(level, iter, e.to, t, min(f, e.capacity));
			if(l == 0) continue;
			e.capacity -= l; g[e.to][e.rev].capacity += l;
			level[u] = lv;
			return l;
		}
		return 0;
	}
};

int main() {
	int H; int W;
	while(~scanf("%d%d", &H, &W)) {
		vector<string> S(H);
		rep(i, H) {
			char buf[101];
			scanf("%s", buf);
			S[i] = buf;
		}
		MaximumFlow mf;
		int src = H * W * 2, src2 = src + 1, dst = src2 + 1;
		mf.init(dst + 1);
		rep(i, H) rep(j, W) {
			static const int dy[4] = { 0, 1, 0, -1 }, dx[4] = { 1, 0, -1, 0 };
			for(int d = 0; d < 4; ++ d) {
				int yy = i + dy[d], xx = j + dx[d];
				if(yy < 0 || yy >= H || xx < 0 || xx >= W) {
					mf.add(H * W + i * W + j, dst, INF);
				} else {
					mf.add(H * W + i * W + j, yy * W + xx, INF);
				}
			}
			if(S[i][j] == 'X') {
				mf.add(src2, i * W + j, INF);
				mf.add(i * W + j, H * W + i * W + j, INF);
			} else {
				mf.add(i * W + j, H * W + i * W + j, 1);
			}
		}
		mf.add(src, src2, INF);
		int ans = mf.maximumFlow(src, dst);
		printf("%d\n", ans == INF ? -1 : ans);
	}
	return 0;
}

Submission Info

Submission Time
Task E - Fences
User anta
Language C++14 (GCC 5.4.1)
Score 150
Code Size 3580 Byte
Status AC
Exec Time 35 ms
Memory 3200 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:83:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
    scanf("%s", buf);
                    ^

Judge Result

Set Name All
Score / Max Score 150 / 150
Status
AC × 19
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 2 ms 256 KB
01_sample.txt AC 2 ms 256 KB
02_sample.txt AC 2 ms 256 KB
10_rand_00.txt AC 2 ms 256 KB
10_rand_01.txt AC 3 ms 384 KB
10_rand_02.txt AC 12 ms 1152 KB
10_rand_03.txt AC 12 ms 1024 KB
10_rand_04.txt AC 12 ms 2816 KB
10_rand_05.txt AC 16 ms 2944 KB
10_rand_06.txt AC 6 ms 1152 KB
10_rand_07.txt AC 14 ms 1920 KB
10_rand_08.txt AC 25 ms 2688 KB
11_hashi_00.txt AC 6 ms 1536 KB
11_hashi_01.txt AC 4 ms 768 KB
11_hashi_02.txt AC 6 ms 1664 KB
12_rect_00.txt AC 35 ms 2560 KB
12_rect_01.txt AC 8 ms 768 KB
12_rect_02.txt AC 12 ms 2176 KB
99_all_one.txt AC 10 ms 3200 KB