(Problem)
给你一张图,有些点可以走,剩下不能走。
只能向下走,走(R*C)或(C*R)。求走完剩下所有可走点的最小路径数。
(Solution)
发现这种题暴力都比较难写,考虑特殊做法(网络流)。
发现当可走点之间连了一条边以后,路径数就会(-1),而且每个点只会被连一次。
可以想到二分图匹配,每个点向可以走的点连边,然后二分图最大匹配的值就是可以连的边数了。
那么答案就是可走点个数-最大匹配值。当然可以用网络流来实现。
(Code)
#include <cstdio>
#include <algorithm>
#define N 110
#define inf 1010580540
#define ll long long
#define mem(x, a) memset(x, a, sizeof x)
#define mpy(x, y) memcpy(x, y, sizeof y)
#define fo(x, a, b) for (int x = (a); x <= (b); x++)
#define fd(x, a, b) for (int x = (a); x >= (b); x--)
#define go(x) for (int p = tail[x], v; p; p = e[p].fr)
using namespace std;
struct node{int v, fr, w;}e[N * N << 3];
int n, m, R, C, num[N][N], tot = 0, S, T, ans = 0;
int tail[N * N << 1], cnt = 1, dis[N * N << 1], gap[N * N << 1];
char s[N][N];
inline int read() {
int x = 0, f = 0; char c = getchar();
while (c < '0' || c > '9') f = (c == '-') ? 1 : f, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f ? -x : x;
}
inline void add(int u, int v, int w) {
// printf("%d %d %d
", u, v, w);
e[++cnt] = (node){v, tail[u], w}; tail[u] = cnt;
e[++cnt] = (node){u, tail[v], 0}; tail[v] = cnt;
}
int dfs(int x, int hav) {
if (x == T) return hav;
int gone = 0, can = 0;
go(x) {
v = e[p].v;
if (! e[p].w || dis[x] != dis[v] + 1) continue;
can = dfs(v, min(hav - gone, e[p].w));
gone += can, e[p].w -= can, e[p ^ 1].w += can;
if (gone == hav) return gone;
}
gap[dis[x]]--;
if (gap[dis[x]] == 0) dis[S] = T;
gap[++dis[x]]++;
return gone;
}
int main()
{
freopen("doll.in", "r", stdin);
freopen("doll.out", "w", stdout);
scanf("%d%d%d%d", &n, &m, &R, &C);
fo(i, 1, n) scanf("%s", s[i] + 1);
fo(i, 1, n) fo(j, 1, m)
if (s[i][j] == '.') num[i][j] = ++tot;
ans = tot;
S = tot + 1, T = tot + 2; tot += 2;
fo(i, 1, n) fo(j, 1, m) {
if (s[i][j] != '.') continue;
add(S, num[i][j], 1), add(tot + num[i][j], T, 1);
if (i + R <= n && j + C <= m && s[i + R][j + C] == '.')
add(num[i][j], tot + num[i + R][j + C], 1);
if (i + R <= n && j - C > 0 && s[i + R][j - C] == '.')
add(num[i][j], tot + num[i + R][j - C], 1);
if (R == C) continue;
if (i + C <= n && j + R <= m && s[i + C][j + R] == '.')
add(num[i][j], tot + num[i + C][j + R], 1);
if (i + C <= n && j - R > 0 && s[i + C][j - R] == '.')
add(num[i][j], tot + num[i + C][j - R], 1);
}
while (dis[S] < T)
ans -= dfs(S, inf);
printf("%d
", ans);
return 0;
}