【链接】 我是链接,点我呀:)
【题意】
【题解】
每个点和相邻的4个点连一条容量为1的边。 然后源点和每头羊连容量INF的边 每头狼和汇点连容量INF的边。 这样求最小割的时候只会把栅栏删掉。 然后源点不能到汇点了。 显然就是每头羊都不能和狼联通了(否则肯定能有增广路a数组一开始写成1万x1万。
(直接超时了。。
【代码】
#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define ms(x,y) memset(x,y,sizeof x)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
const int N = 10000;
const int INF = 10000+10;
struct abc{
int en,nex;
LL flow;
};
int n,m,s,t,cost[N+10],deep[N*2+20];
int fir[N+10],tfir[N+10],totm;
abc bian[2*N+2*N+N*8+10];
queue <int> dl;
void add(int x,int y,LL cost){
bian[totm].nex = fir[x];
fir[x] = totm;
bian[totm].en = y,bian[totm].flow = cost;
totm++;
bian[totm].nex = fir[y];
fir[y] = totm;
bian[totm].en = x,bian[totm].flow = 0;
totm++;
}
bool bfs(int s,int t){
dl.push(s);
ms(deep,255);
deep[s] = 0;
while (!dl.empty()){
int x = dl.front();
dl.pop();
for (int temp = fir[x]; temp!= -1 ;temp = bian[temp].nex){
int y = bian[temp].en;
if (deep[y]==-1 && bian[temp].flow>0){
deep[y] = deep[x] + 1;
dl.push(y);
}
}
}
return deep[t]!=-1;
}
LL dfs(int x,int t,LL limit){
if (x == t) return limit;
if (limit == 0) return 0;
LL cur,f = 0;
for (int temp = tfir[x];temp!=-1;temp = bian[temp].nex){
tfir[x] = temp;
int y = bian[temp].en;
if (deep[y] == deep[x] + 1 && (cur = dfs(y,t,min(limit,(LL)bian[temp].flow))) ){
f += cur;
limit -= cur;
bian[temp].flow -= cur;
bian[temp^1].flow += cur;
if (!limit) break;
}
}
return f;
}
int a[100+10][100+10];
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ms(fir,255);
scanf("%d%d",&n,&m);
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
scanf("%d",&a[i][j]);
for (int i = 1;i <= n;i++){
for (int j = 1;j <= m;j++){
for (int k = 0;k < 4;k++){
int ti = i+dx[k],tj = j+dy[k];
if (ti>=1 && ti<=n && tj>=1 && tj<=m){
add((i-1)*m+j,(ti-1)*m+tj,1);
}
}
}
}
for (int i = 1;i <= n;i++)
for (int j = 1;j <=m;j++){
if (a[i][j]==1){
add(0,(i-1)*m+j,INF);
}else if (a[i][j]==2){
add((i-1)*m+j,n*m+1,INF);
}
}
s = 0,t = n*m+1;
int ans = 0;
while ( bfs(s,t) ){
for (int i = 0;i <= n*m+1;i++) tfir[i] = fir[i];
ans += dfs(s,t,INF);
}
printf("%d
",ans);
return 0;
}