Pleasant sheep and big big wolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2742 Accepted Submission(s): 1128
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
只想说网络流的问题,构图想不到啊。
那种心情,无法言语。
只贴代码。
那种心情,无法言语。
只贴代码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
const int maxn = 40000+10;
const int inf = 0x3f3f3f3f;
using namespace std;
int dir[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int T=0;
int level[maxn];
int head[1000000+10];
struct node {
int t, f, next;
}edge[1000000+10];
int cnt;
void init() {
memset(level, -1, sizeof(level));
memset(head, -1, sizeof(level));
cnt=0;
}
void addedge(int u, int v, int f) {
edge[cnt].t = v;
edge[cnt].f = f;
edge[cnt].next = head[u];
head[u] = cnt;
cnt++;
}
bool bfs(int s, int t) {
queue<int>q;
q.push(s);
memset(level, 0, sizeof(level));
level[s] = 1;
while (!q.empty()) {
int now = q.front();
q.pop();
if (now == t)
return true;
for (int i=head[now]; i!=-1; i=edge[i].next) {
if (!level[edge[i].t] && edge[i].f) {
level[edge[i].t] = level[now]+1;
q.push(edge[i].t);
}
}
}
return false;
}
int max_flow(int u, int maxf, int t) {
if (t == u)
return maxf;
int ret = 0;
for (int i=head[u]; i!=-1; i=edge[i].next) {
if (level[edge[i].t] == level[u]+1) {
int f = max_flow(edge[i].t, min(maxf-ret, edge[i].f), t);
edge[i].f -= f;
edge[i^1].f += f;
ret += f;
if (ret == maxf)
return ret;
}
}
return ret;
}
int maxflow(int s, int t) {
int flow = 0;
while (bfs(s, t)==true)
flow += max_flow(s, inf, t);
return flow;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n, m;
while (scanf("%d%d", &n, &m)!=EOF) {
int s = 0;
T++;
int t = n*m+1;
init();
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
int a;
scanf("%d", &a);
int u = (i-1)*m+j;
if (a == 1) {
addedge(s, u, inf);
addedge(u, s, inf);
}
else if (a == 2) {
addedge(u, t, inf);
addedge(t, u, inf);
}
for (int k=0; k<2; k++) { //形成的边相互的只要两个方向。
int nx = dir[k][0]+i;
int ny = dir[k][1]+j;
if (nx <= 0 || nx > n)
continue;
if (ny <= 0 || ny > m)
continue;
int v = (nx-1)*m+ny;
addedge(u, v, 1);
addedge(v, u, 1);
}
}
}
printf("Case %d:\n%d\n", T, maxflow(s, t));
}
return 0;
}