题意:在一个单位方格边长为1的矩阵中藏着灰太狼和它的同伴,等待着喜羊羊和它的同伴,为了不让喜羊羊和同伴被抓住,我们可以在矩形草坪中设置单位长度为1的栅栏,求最短的栅栏长度。

解题思路:这道题是要把狼和羊分开,如果熟悉最小割的话,就比较容易了,将所有的羊同源点连一条无穷大的边,狼和汇点连一条无穷大的边,剩下的格子与周围格子连一条容量为1的边,接下来就是求最小割即可。主要是为什么这样建图就是对的,首先是容量为1的边,这个比较好理解,两个相邻格子只要加一个栅栏就可以隔开了。接下来是无穷大的边,因为羊的限制是由它周围的格子决定的,与源点无关,而且回顾最小割,羊与源点的边集肯定不会在最小割集里,这样也符合实际问题。

#include <cstring>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#define INF 0x3fffffff
#define MAXN 205
using namespace std;

int N, M, G[MAXN][MAXN], dis[MAXN*MAXN];
int dir[2][2] = {0, 1, 1, 0};
int idx, head[MAXN*MAXN];
const int sink = MAXN*MAXN-1, source = MAXN*MAXN-2;

struct Edge
{
    int v, cap, next;
}e[MAXN*MAXN*4];

inline void init()
{ 
    idx = -1;
    memset(head, 0xff, sizeof (head));
}

inline int to(int x, int y)
{
    return (x-1)*M+(y-1);
}

inline void insert(int a, int b, int c)
{
    ++idx;
    e[idx].v = b, e[idx].cap = c;
    e[idx].next = head[a], head[a] = idx;
}

inline bool judge(int x, int y)
{
    if (x < 1 || x > N || y < 1 || y > M) {
        return false;
    }
    return true;
}

inline void check(int x, int y)
{
    int xx, yy;
    if (G[x][y] == 1) {
        insert(to(x, y), sink, INF);
        insert(sink, to(x, y), 0); 
    }
    else if (G[x][y] == 2) {
        insert(source, to(x, y), INF);
        insert(to(x, y), source, 0); 
    }  
    for (int i = 0; i < 2; ++i) {
        xx = x + dir[i][0], yy = y + dir[i][1];
        if (judge(xx, yy)) {
            insert(to(x, y), to(xx, yy), 1);
            insert(to(xx, yy), to(x, y), 1);
        }
    }
}

bool bfs()
{
    int pos;
    queue<int>q;
    memset(dis, 0xff, sizeof (dis));
    dis[source] = 0;
    q.push(source);
    while (!q.empty()) {
        pos = q.front();
        q.pop();
        for (int i = head[pos]; i != -1; i = e[i].next) {
            if (dis[e[i].v] == -1 && e[i].cap > 0) {
                dis[e[i].v] = dis[pos]+1;
                q.push(e[i].v);
            }
        }
    }
    return dis[sink] != -1;
}

int dfs(int u, int flow)
{
    if (u == sink) {
        return flow;
    }
    int tf = 0, sf;
    for (int i = head[u]; i != -1; i = e[i].next) {
        if (dis[u]+1 == dis[e[i].v] && e[i].cap > 0 && (sf = dfs(e[i].v, min(flow-tf, e[i].cap)))) {
            e[i].cap -= sf, e[i^1].cap += sf;
            tf += sf;
            if (tf == flow) {
                return flow;
            }
        }
    }
    if (!tf) {
        dis[u] = -1;
    }
    return tf;
}

int dinic()
{
    int ans = 0;
    while (bfs()) {
        ans += dfs(source, INF);
    }
    return ans;
}

int main()
{   
    int ca = 0;
    while (scanf("%d %d", &N, &M) == 2) {
        init();
        for (int i = 1; i <= N; ++i) {
            for (int j = 1; j <= M; ++j) {
                scanf("%d", &G[i][j]);
            }
        }
        for (int i = 1; i <= N; ++i) {
            for (int j = 1; j <= M; ++j) {
                check(i, j);
            }
        }
        printf("Case %d:\n%d\n", ++ca, dinic());
    }
    return 0;
}