BZOJ 1066: [SCOI2007]蜥蜴

二次联通门 : BZOJ 1066: [SCOI2007]蜥蜴

 

 

 

 

 

/*
    BZOJ 1066: [SCOI2007]蜥蜴

    拆点最大流

*/
#include <cstdio>
#include <iostream>
#define rg register

inline void read (int &n) { 
    rg char c = getchar ();
    for (n = 0; !isdigit (c); c = getchar ());
    for (; isdigit (c); n = n * 10 + c - '0', c = getchar ());
}
#define Max 900
int S, T;
#define INF 1e9
inline int abs (int x) { return x < 0 ? -x : x; }
namespace net {

    const int MaxE = 200005;
    int _n[MaxE], _v[MaxE], list[Max], _f[MaxE], EC = 1, d[Max], q[Max], tc[Max];
    
    inline void In (int u, int v, int f) { 
        _v[++ EC] = v, _n[EC] = list[u], list[u] = EC, _f[EC] = f;
        _v[++ EC] = u, _n[EC] = list[v], list[v] = EC, _f[EC] = 0;
    }

    bool Bfs () {
        int h = 1, t = 1; q[t] = S; rg int i, n;
        for (i = 0; i <= T; ++ i) d[i] = -1;
        for (d[S] = 0; h <= t; ++ h)
            for (n = q[h], i = list[n]; i; i = _n[i])
                if (_f[i] && d[_v[i]] < 0) {
                    d[_v[i]] = d[n] + 1, q[++ t] = _v[i];
                    if (_v[i] == T) return true; 
                }
        return false;
    }
    int Flowing (int n, int f) {
        if (n == T || f == 0) return f;
        int p, r = 0;
        for (rg int &i = tc[n]; i; i = _n[i])
            if (_f[i] && d[_v[i]] == d[n] + 1) {
                p = Flowing (_v[i], std :: min (_f[i], f));
                if (p > 0) { 
                    r += p, f -= p, _f[i] -= p, _f[i ^ 1] += p;
                    if (f == 0) return r;
                }
            }
        if (r != f) d[n] = -1; return r;
    }

    int Dinic () { 
        int res = 0;
        for (; Bfs (); res += Flowing (S, INF))
            for (rg int i = 0; i <= T; ++ i) tc[i] = list[i];
        return res;
    }
}

char s[Max]; 
int a[Max][Max], t[Max][Max];
inline int max (int a, int b) { return a > b ? a : b; }
inline int min (int a, int b) { return a < b ? a : b; }
int main (int argc, char *argv[]) {
    
    int R, C, D; read (R), read (C), read (D); 
    rg int i, j, k, e; S = 0, T = 2 * R * C + 1;
    int A, B, E = 0, c = 0;
    for (i = 1; i <= R; ++ i) {
        scanf ("%s", s + 1);
        for (j = 1; s[j]; ++ j) 
            if ((a[i][j] = s[j] - '0'))
                t[i][j] = ++ c, net :: In (c, c + R * C, a[i][j]);
    }
    for (i = 1; i < R; ++ i)
        for (j = 1; j < C; ++ j)
            if (a[i][j])
                for (k = max (i - D, 1), A = min (i + D, R); k <= A; ++ k)
                    for (e = max (j - D, 1), B = min (j + D, C); e <= B; ++ e)
                        if (abs (k - i) + abs (e - j) <= D && a[k][e] && (k != i || e != j))
                            net :: In (t[i][j] + R * C, t[k][e], INF);
    for (i = 1; i <= R; ++ i)
        for (j = 1; j <= C; ++ j)
            if (a[i][j] && (i <= D || j <= D || R + 1 - i <= D || C + 1 - j <= D))
                net :: In (t[i][j] + R * C, T, INF);
    
    for (i = 1; i <= R; ++ i) {
        scanf ("%s", s + 1);
        for (j = 1; s[j]; ++ j)
            if (s[j] == 'L') ++ E, net :: In (S, t[i][j], 1);
    }
    printf ("%d", E - net :: Dinic ());        
    
    return 0;
}

 

posted @ 2018-01-19 08:36  ZlycerQan  阅读(174)  评论(0编辑  收藏  举报