QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#797464#9224. Express EvictionlotusblumeWA 0ms4484kbC++202.9kb2024-12-03 03:50:082024-12-03 03:50:09

Judging History

你现在查看的是最新测评结果

  • [2024-12-03 03:50:09]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:4484kb
  • [2024-12-03 03:50:08]
  • 提交

answer

#include <algorithm>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

struct Dinic {
	struct Edge {
		int to, rev;
		ll c, oc;
		// ll flow() { return max(oc - c, 0LL); }
	};
	vector<vector<Edge>> adj;
	vector<int> lvl, ptr, que;
	Dinic(int n) : adj(n), lvl(n), ptr(n), que(n) {}
	// assert: 0 <= a, b < n; a != b; 0 <= c
	void add_edge(int a, int b, ll c) {
		adj[a].push_back({b, (int) adj[b].size(), c, c});
		adj[b].push_back({a, (int) adj[a].size() - 1, 0, 0});
	}
	ll flow(int s, int t) {
		ll ans = 0;
		while (bfs(s, t)) {
			fill(ptr.begin(), ptr.end(), 0);
			ans += dfs(s, t, numeric_limits<ll>::max());
		}
		return ans;
	}
	bool bfs(int s, int t) { // Hash: 063179
		fill(lvl.begin(), lvl.end(), -1);
		lvl[t] = 0; que[0] = t;
		int qi = 0, qe = 1;
		while (qi < qe) {
			int v = que[qi++];
			for (Edge e : adj[v]) {
				if (lvl[e.to] != -1) continue;
				if (!adj[e.to][e.rev].c) continue;
				lvl[e.to] = lvl[v] + 1;
				if (e.to == s) return true;
				que[qe++] = e.to;
			}
		}
		return false;
	}
	ll dfs(int v, int t, ll up) { // Hash: e7ef5c
		if (v == t || up == 0) return up;
		ll res = 0;
		for (; ptr[v] < (int) adj[v].size(); ++ptr[v]) {
			Edge& e = adj[v][ptr[v]];
			if (lvl[e.to] + 1 != lvl[v]) continue;
			ll d = dfs(e.to, t, min(up - res, e.c));
			e.c -= d; adj[e.to][e.rev].c += d;
			res += d;
			if (res == up) return res;
		}
		return res;
	}
	// bool left_of_mincut(int a) { return (lvl[a] != -1); }
};

const int INF = 1000005;

int main() {
	int n, m;
	std::cin >> n >> m;

	std::vector<std::string> grid(n);

	for (int i = 0; i < n; ++i) {
		std::cin >> grid[i];
	}

	int s = 2 * n * m;
	int t = s + 1;

	Dinic g(s + 2);

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (grid[i][j] == '#') {
				g.add_edge(2 * (i * m + j), 2 * (i * m + j) + 1, 1);
			}
		}
	}

	for (int i = 0; i < n; ++i) {
		if (grid[i][0] == '#' || 1) {
			g.add_edge(s, 2 * i * m, INF);
		}
		if (grid[i][m - 1] == '#' || 1) {
			g.add_edge(2 * (i * m + m - 1) + 1, t, INF);
		}
	}

	for (int j = 1; j < m; ++j) {
		if (grid[n - 1][j] == '#' || 1) {
			g.add_edge(s, 2 * ((n - 1) * m + j), INF);
		}
	}

	for (int j = 0; j < m - 1; ++j) {
		if (grid[0][j] == '#' || 1) {
			g.add_edge(2 * j + 1, t, INF);
		}
	}

	std::vector<int> dx = {0, 1, 0, -1, 1, 1, -1, -1};
	std::vector<int> dy = {1, 0, -1, 0, 1, -1, -1, 1};

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			for (int dir = 0; dir < 8; ++dir) {
				int i2 = i + dx[dir];
				int j2 = j + dy[dir];
				if (i2 < 0 || i2 >= n || j2 < 0 || j2 >= m) continue;
				g.add_edge(2 * (i * m + j) + 1, 2 * (i2 * m + j2), INF);
				g.add_edge(2 * (i2 * m + j2) + 1, 2 * (i * m + j), INF);
			}
		}
	}

	std::cout << g.flow(s, t) << '\n';

	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3652kb

input:

4 6
.##...
.#....
##....
....#.

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 4484kb

input:

20 30
...###########################
#..###########################
...###########################
...###########################
.#############################
...###########################
...###########################
#..###########################
...###########################
..#...............

output:

7

result:

wrong answer 1st numbers differ - expected: '11', found: '7'