# AtCoder Regular Contest 074 F - Lotus Leaves 最小割

2<=n,m<=100

### 代码

#include <bits/stdc++.h>
#define num(i,j,k) ((((i)-1)*m+(j))*2+k)
using namespace std;
const int N = 1000010;
const int inf = 1e9;
{
int p=0; int f=1; char ch=getchar();
while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();}
while(ch>='0' && ch<='9'){p=p*10+ch-'0'; ch=getchar();}
return p*f;
}
struct node
{
int x,y,c,next;
}edge[N]; int len,first[N];
void ins(int x,int y,int c)
{
len++; edge[len].x=x; edge[len].y=y; edge[len].c=c; edge[len].next=first[x]; first[x]=len;
len++; edge[len].x=y; edge[len].y=x; edge[len].c=0; edge[len].next=first[y]; first[y]=len;
}

int n,m; char str[310][310]; int s,d; int tot;

queue<int>q; int dep[N];

bool bfs()
{
while(!q.empty()) q.pop(); q.push(s);
for(int i=1;i<=tot+n+m;i++) dep[i] = 0; dep[s] = 1;
while(!q.empty())
{
int x = q.front();
for(int k=first[x];k!=-1;k=edge[k].next)
{
int y = edge[k].y;
if(dep[y] == 0 && edge[k].c)
{
dep[y] = dep[x] + 1;
q.push(y);
}
}
q.pop();
}
return dep[d] > 0;
}

int dfs(int x,int flow)
{
if(x==d || flow == 0) return flow;
int delta=0;
for(int k=first[x];k!=-1;k=edge[k].next)
{
int y = edge[k].y;
if(dep[y] == dep[x] + 1 && edge[k].c && flow > delta)
{
int minf = dfs(y,min(flow - delta , edge[k].c));
edge[k].c-=minf; edge[k^1].c+=minf;
delta+=minf;
}
}
if(delta==0) dep[x] = 0;
return delta;
}

int main()
{
len = 1; memset(first,-1,sizeof(first));

for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
{
scanf("\n%c",&str[i][j]);
if(str[i][j] == 'S') s = num(i,j,0);
else if(str[i][j] == 'T') d = num(i,j,1);
}

tot = num(n,m,1);
for(int i=1;i<=n;i++) // tot + i
{
for(int j=1;j<=m;j++) // tot + n + j
{
if(str[i][j] == '.') continue;
if(str[i][j] == 'o') ins(num(i,j,0) , num(i,j,1) , 1);
else ins(num(i,j,0) , num(i,j,1) , inf);
ins(num(i,j,1) , tot+i , inf);
ins(num(i,j,1) , tot+n+j , inf);
ins(tot+i , num(i,j,0) , inf);
ins(tot+n+j , num(i,j,0) , inf);
// printf("(%d,%d) ,  %d %d\n",i,j,i,j);
}
}

int ans = 0;
while(bfs())
ans+=dfs(s,inf);
return printf("%d\n",(ans >= inf) ? -1 : ans),0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120