Pleasant sheep and big big wolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1851 Accepted Submission(s): 792
Problem Description
In ZJNU, there is a well-known prairie. And it attracts pleasant sheep and his companions to have a holiday. Big big wolf and his families know about this, and quietly hid in the big lawn. As ZJNU ACM/ICPC team, we have an obligation
to protect pleasant sheep and his companions to free from being disturbed by big big wolf. We decided to build a number of unit fence whose length is 1. Any wolf and sheep can not cross the fence. Of course, one grid can only contain an animal.
Now, we ask to place the minimum fences to let pleasant sheep and his Companions to free from being disturbed by big big wolf and his companions.

Now, we ask to place the minimum fences to let pleasant sheep and his Companions to free from being disturbed by big big wolf and his companions.

Input
There are many cases.
For every case:
N and M(N,M<=200)
then N*M matrix:
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
For every case:
N and M(N,M<=200)
then N*M matrix:
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
Output
For every case:
First line output “Case p:”, p is the p-th case;
The second line is the answer.
First line output “Case p:”, p is the p-th case;
The second line is the answer.
Sample Input
4 6 1 0 0 1 0 0 0 1 1 0 0 0 2 0 0 0 0 0 0 2 0 1 1 0
Sample Output
Case 1: 4题意:有若干个羊和狼,现在要建造一些长度为1的栅栏,把狼和羊隔开。求最小要建造多少栅栏。思路:求最小割,源点连一条边到各个狼,容量为INF,每个羊连边到汇点,容量也为INF(保证了最小割上的边全部为容量为1的边),然后每个点向它四周的点连边,容量为1。最后求最大流即可。AC代码:#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <ctime> #include <algorithm> #define ll __int64 using namespace std; const int INF = 1000000000; const int maxn = 40005; int d[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; struct Edge{ int u, v, cap, flow, next; }et[maxn*100]; int low[maxn], dis[maxn], cnt[maxn], cur[maxn], pre[maxn], eh[maxn]; int n, m, s, t, num; int G[205][205]; bool ok(int x, int y){ if(x >= 1 && x <= n && y >= 1 && y <= m) return true; return false; } void init(){ memset(eh, -1, sizeof(eh)); num = 0; } void add(int u, int v, int cap, int flow){ Edge e = {u, v, cap, flow, eh[u]}; et[num] = e; eh[u] = num++; } void addedge(int u, int v, int cap){ add(u, v, cap, 0); add(v, u, 0, 0); } int isap(int s, int t, int nv){ int u, v, now, flow = 0; memset(cnt, 0, sizeof(cnt)); memset(dis, 0, sizeof(dis)); memset(low, 0, sizeof(low)); for(u = 0; u <= nv; u++) cur[u] = eh[u]; low[s] = INF, cnt[0] = nv, u = s; while(dis[s] < nv) { for(now = cur[u]; now != -1; now = et[now].next) if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break; if(now != -1) { cur[u] = pre[v] = now; low[v] = min(et[now].cap - et[now].flow, low[u]); u = v; if(u == t) { for(; u != s; u = et[pre[u]].u) { et[pre[u]].flow += low[t]; et[pre[u]^1].flow -= low[t]; } flow += low[t]; low[s] = INF; } } else { if(--cnt[dis[u]] == 0) break; dis[u] = nv, cur[u] = eh[u]; for(now = eh[u]; now != -1; now = et[now].next) if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1) dis[u] = dis[et[now].v] + 1; cnt[dis[u]]++; if(u != s) u = et[pre[u]].u; } } return flow; } int main() { int ca = 0; while(~scanf("%d%d", &n, &m)) { init(); s = 0; t = n * m + 1; 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++) { for(int k = 0; k < 4; k++) { int nx = i + d[k][0], ny = j + d[k][1]; if(ok(nx, ny)) addedge((i - 1) * m + j, (nx - 1) * m + ny, 1); } if(G[i][j] == 2) addedge(s, (i - 1) * m + j, INF); else if(G[i][j] == 1) addedge((i - 1) * m + j, t, INF); } printf("Case %d:\n", ++ca); printf("%d\n", isap(s, t, t + 1)); } return 0; }