题目大意
一个池塘,有起点叶子、终点叶子和叶子。
同行同列的叶子联通,问最少删去多少叶子(除起点终点)能使得起点和终点不连通。
最小割
显然
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int maxn=100+10,maxm=1000000+10,inf=1000000000;
int h[maxn*2],now[maxn*2],d[maxn*2],go[maxm*2],dis[maxm*2],fx[maxm*2],nxt[maxm*2];
bool bz[maxn*2];
int i,j,k,l,t,n,m,tot,top,tmp,sx,sy,tx,ty,s,x,y;
char ch;
char get(){
char ch=getchar();
while (ch!='.'&&ch!='o'&&ch!='S'&&ch!='T') ch=getchar();
return ch;
}
void add(int x,int y,int z,int d){
go[++tot]=y;
dis[tot]=z;
fx[tot]=tot+d;
nxt[tot]=h[x];
h[x]=tot;
}
int dfs(int x,int flow){
if (x==t){
tmp+=flow;
return flow;
}
bz[x]=1;
int r=now[x],k;
while (r){
if (!bz[go[r]]&&dis[r]&&d[x]==d[go[r]]+1){
k=dfs(go[r],min(dis[r],flow));
if (k){
dis[r]-=k;
dis[fx[r]]+=k;
now[x]=r;
return k;
}
}
r=nxt[r];
}
return now[x]=0;
}
bool change(){
int i,r,tmp=inf;
fo(i,s,t)
if (bz[i]){
r=h[i];
while (r){
if (!bz[go[r]]&&dis[r]&&d[go[r]]+1-d[i]<tmp) tmp=d[go[r]]+1-d[i];
r=nxt[r];
}
}
if (tmp==inf) return 0;
fo(i,s,t)
if (bz[i]) d[i]+=tmp;
return 1;
}
int main(){
scanf("%d%d",&n,&m);
fo(i,1,n)
fo(j,1,m){
ch=get();
if (ch=='S') sx=i,sy=j;
else if (ch=='T') tx=i,ty=j;
else if (ch=='o'){
add(i+1,j+n+1,1,1);
add(j+n+1,i+1,0,-1);
add(j+n+1,i+1,1,1);
add(i+1,j+n+1,0,-1);
}
}
s=1;t=n+m+2;
add(s,sx+1,inf,1);
add(sx+1,s,0,-1);
add(s,sy+n+1,inf,1);
add(sy+n+1,s,0,-1);
add(tx+1,t,inf,1);
add(t,tx+1,0,-1);
add(ty+n+1,t,inf,1);
add(t,ty+n+1,0,-1);
do{
fo(i,s,t) now[i]=h[i];
fill(bz+s,bz+t+1,0);
while (dfs(s,inf)) fill(bz+s,bz+t+1,0);
}while (change());
if (tmp>=inf) printf("-1\n");else printf("%d\n",tmp);
}