#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 2700
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
int to,val,next;
}edge[N*N];
int head[N],dep[N],map[N][N];
int n,a1,a2,an,b1,b2,bn,cnt;
char s[N];
void init()
{
memset(head,-1,sizeof(head));cnt=0;
}
void edgeadd(int from,int to,int val)
{
edge[cnt].to=to;
edge[cnt].val=val;
edge[cnt].next=head[from];
head[from]=cnt++;
}
int bfs(int s,int e)
{
memset(dep,0,sizeof(dep));
queue<int>q;
q.push(s);
dep[s]=1;
while(!q.empty())
{
int u=q.front();
if(u==e)return 1;
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(edge[i].val==0||dep[to]!=0)continue;
dep[to]=dep[u]+1;
q.push(to);
}
}
return 0;
}
int dfs(int s,int max_vale)
{
int ret=0,tmp;
if(s==n*n+1)return max_vale;
for(int i=head[s];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(edge[i].val==0||dep[to]!=dep[s]+1)continue;
tmp=dfs(to,min(max_vale-ret,edge[i].val));
edge[i].val-=tmp;
edge[i^1].val+=tmp;
ret+=tmp;
if(ret==max_vale)return ret;
}
return ret;
}
void build()
{
init();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(map[i][j]==1)
{
edgeadd(i,j,INF);
edgeadd(j,i,0);
}else if(map[i][j]==2)
{
edgeadd(i,j,1);
edgeadd(j,i,0);
}
}
}
}
int main()
{
// freopen("bridge.in","r",stdin);
// freopen("bridge.out","w",stdout);
while(~scanf("%d%d%d%d%d%d%d",&n,&a1,&a2,&an,&b1,&b2,&bn))
{
memset(map,0,sizeof(map));
init();
a1++,a2++,b1++,b2++;
for(int i=1;i<=n;i++)
{
scanf("%s",s);
for(int j=0;j<n;j++)
{
if(s[j]==‘N‘)
{
map[i][j+1]=1;
}else if(s[j]==‘O‘)
{
map[i][j+1]=2;
}
}
}
build();
edgeadd(0,a1,1*an);
edgeadd(a1,0,0);
edgeadd(a2,n*n+1,1*an);
edgeadd(n*n+1,a2,0);
edgeadd(0,b1,1*bn);
edgeadd(b1,0,0);
edgeadd(b2,n*n+1,1*bn);
edgeadd(n*n+1,b2,0);
int ret=0,flag=0;
while(bfs(0,n*n+1))
{
while(int t=dfs(0,INF))
{
ret+=t;
}
}
if(ret<1*(an+bn)){flag=1;}
if(!flag)
{
build();
edgeadd(0,a1,1*an);
edgeadd(a1,0,0);
edgeadd(a2,n*n+1,1*an);
edgeadd(n*n+1,a2,0);
edgeadd(0,b2,1*bn);
edgeadd(b2,0,0);
edgeadd(b1,n*n+1,1*bn);
edgeadd(n*n+1,b1,0);
ret=0;
while(bfs(0,n*n+1))
{
while(int t=dfs(0,INF))
{
ret+=t;
}
}
if(ret<1*(an+bn)){flag=1;}
}
if(flag)printf("No\n");
else printf("Yes\n");
}
}
原文地址:http://blog.csdn.net/wzq_qwq/article/details/46546977