Pleasant sheep and big big wolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3257 Accepted Submission(s): 1339
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.
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.
Output
For every case:
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
思路:原点与所有羊连接流量为INF的边,汇点与所有狼连接流量为INF的边,每个格子与周围的格子连接流量为1的边,跑最小割
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 40500;
const int MAXM = 250005;
const int INF = 0x3f3f3f3f;
struct Edge1
{
int from,to,cap,flow;
};
struct Dinic
{
int n,m,s,t;
vector<Edge1> edges;
vector<int> G[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void init(int n)
{
this -> n = n;
for(int i = 0; i <= n + 1; i++){
G[i].clear();
}
edges.clear();
}
void AddEdge(int from,int to,int cap)
{
edges.push_back((Edge1){from,to,cap,0});
edges.push_back((Edge1){to,from,0,0});
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS()
{
memset(vis,0,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while(!Q.empty()) {
int x = Q.front();
Q.pop();
for(int i = 0; i < G[x].size(); i++) {
Edge1& e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
if(x == t || a == 0) return a;
int flow = 0,f;
for(int& i = cur[x]; i < G[x].size(); i++) {
Edge1& e = edges[G[x][i]];
if(d[x] + 1 == d[e.to] && (f = DFS(e.to,min(a,e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
int Maxflow(int s,int t) {
this -> s = s,this -> t = t;
int flow = 0;
while(BFS()) {
memset(cur,0,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
}din;
int main(void)
{
int n,m,temp,src,des,id;
int kase = 0;
while(scanf("%d %d",&n,&m) != EOF) {
kase++;
src = 0,des = n * m + 1;
din.init(n * m + 2);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
id = (i - 1) * m + j;
scanf("%d",&temp);
if(temp == 1) din.AddEdge(src,id,INF);
else if(temp == 2) din.AddEdge(id,des,INF);
if(i > 1) din.AddEdge(id,id - m,1);
if(j > 1) din.AddEdge(id,id - 1,1);
if(i < n) din.AddEdge(id,id + m,1);
if(j < m) din.AddEdge(id,id + 1,1);
}
}
printf("Case %d:\n%d\n",kase,din.Maxflow(src,des));
}
return 0;
}
/*
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
*/