网络流
一个点拆成两个,注意要把某一类边连反过来
这样才能保证有限制
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
# define Copy(a, b) memcpy(a, b, sizeof(a))
# define ID(a, b) (a - 1) * c + b
# define Sqr(x) ((x) * (x))
# define Dis(i, j, x, y) (Sqr(i - x) + Sqr(j - y))
using namespace std;
typedef long long ll;
const int _(1010), __(6e6 + 10), INF(2147483647);
IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int num, r, c, d, high[21][21], w[__], fst[_], nxt[__], to[__], cnt, S, T, lev[_], cur[_], max_flow;
queue <int> Q;
IL void Add(RG int u, RG int v, RG int f){
w[cnt] = f; to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;
w[cnt] = 0; to[cnt] = u; nxt[cnt] = fst[v]; fst[v] = cnt++;
}
IL int Dfs(RG int u, RG int maxf){
if(u == T) return maxf;
RG int ret = 0;
for(RG int &e = cur[u]; e != -1; e = nxt[e]){
if(lev[to[e]] != lev[u] + 1 || !w[e]) continue;
RG int f = Dfs(to[e], min(w[e], maxf - ret));
ret += f; w[e ^ 1] += f; w[e] -= f;
if(ret == maxf) break;
}
if(!ret) lev[u] = 0;
return ret;
}
IL bool Bfs(){
Fill(lev, 0); lev[S] = 1; Q.push(S);
while(!Q.empty()){
RG int u = Q.front(); Q.pop();
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(lev[to[e]] || !w[e]) continue;
lev[to[e]] = lev[u] + 1;
Q.push(to[e]);
}
}
return lev[T];
}
int main(RG int argc, RG char* argv[]){
r = Read(); c = Read(); d = Read(); Fill(fst, -1); T = r * c * 2 + 1;
for(RG int i = 1; i <= r; ++i)
for(RG int j = 1; j <= c; ++j){
RG char cc; scanf(" %c", &cc);
high[i][j] = (cc ^ 48);
if(!high[i][j]) continue;
Add(ID(i, j), ID(i, j) + r * c, high[i][j]);
if(i <= d || j <= d || r - i < d || c - j < d) Add(ID(i, j) + r * c, T, INF);
}
for(RG int i = 1; i <= r; ++i)
for(RG int j = 1; j <= c; ++j){
RG char cc; scanf(" %c", &cc);
for(RG int x = 1; x <= r; ++x)
for(RG int y = 1; y <= c; ++y)
if((x != i || y != j) && high[x][y] && Dis(i, j, x, y) <= d * d)
Add(ID(i, j) + r * c, ID(x, y), INF);
if(cc != 'L') continue;
Add(S, ID(i, j), 1); num++;
}
while(Bfs()) Copy(cur, fst), max_flow += Dfs(S, INF);
printf("%d\n", num - max_flow);
return 0;
}