首页 > 其他 > 详细

BZOJ 1412: [ZJOI2009]狼和羊的故事

时间:2018-01-19 23:13:54      阅读:270      评论:0      收藏:0      [点我收藏+]

二次联通门 : BZOJ 1412: [ZJOI2009]狼和羊的故事

 

 

 

 

 

/*
    BZOJ 1412: [ZJOI2009]狼和羊的故事

    最小割
    果然lqz只能水这种题。。。
*/
#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 ());
}
int S, T;
#define Max 7000
#define INF 1e9
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;
    }
}
int a[Max / 60][Max / 60];
int main (int argc, char *argv[]) { 
        
    int N, M, p; read (N), read (M); rg int i, j;
    S = 1, T = N * M + 2;
    for (i = 1; i <= N; ++ i)
        for (j = 1; j <= N; ++ j) read (a[i][j]);
    for (i = 1; i <= N; ++ i)
        for (j = 1; j <= M; ++ j) {
            p = (i - 1) * M + j;
            if (a[i][j] == 1) net :: In (S, S + p, INF);
            else if (a[i][j] == 2) net :: In (S + p, T, INF);
            if (i != 1) net :: In (S + p, S + (i - 2) * M + j, 1);
            if (j != 1) net :: In (S + p, p, 1);
            if (i != N) net :: In (S + p, 1 + i * M + j, 1);
            if (j != M) net :: In (S + p, 2 + p, 1);
        }
    printf ("%d", net :: Dinic ());

    return 0;
}

 

BZOJ 1412: [ZJOI2009]狼和羊的故事

原文:https://www.cnblogs.com/ZlycerQan/p/8318971.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!