1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #define M 100009
  5 #define inf 2139062143
  6 using namespace std;
  7 int n,a1,a2,an,b1,b2,bn,tot,cnt=1,T,ans,head[M],d[M],q[2*M],next[10*M],u[10*M],v[10*M];
  8 char ch[60][60];
  9 bool bfs()
 10 {
 11     memset(d,0,sizeof(int)*(T+1));
 12     int h=0,t=1;
 13     q[1]=0;
 14     d[0]=1;
 15     for(;h<t;)
 16       {
 17         h++;
 18         int p=q[h];
 19         for(int i=head[p];i;i=next[i])
 20           if(!d[u[i]]&&v[i])
 21             {
 22                 d[u[i]]=d[p]+1;
 23                 if(d[T])
 24                   return 1;
 25                 t++;
 26                 q[t]=u[i];
 27             }
 28       }
 29     return 0;
 30 }
 31 int dinic(int s,int f)
 32 {
 33     if(s==T)
 34       return f;
 35     int rest=f;
 36     for(int i=head[s];i&&rest;i=next[i])
 37       if(v[i]&&d[u[i]]==d[s]+1)
 38         {
 39             int now=dinic(u[i],min(rest,v[i]));
 40             if(!now)
 41               d[u[i]]=0;
 42             v[i]-=now;
 43             v[i^1]+=now;
 44             rest-=now;
 45         }
 46     return f-rest;  
 47 }
 48 void jia1(int a1,int a2,int a3)
 49 {
 50     cnt++;
 51     next[cnt]=head[a1];
 52     head[a1]=cnt;
 53     u[cnt]=a2;
 54     v[cnt]=a3;
 55     return;
 56 }
 57 void jia(int a1,int a2,int a3)
 58 {
 59     jia1(a1,a2,a3);
 60     jia1(a2,a1,0);
 61     return;
 62 }
 63 void build(int a1,int a2,int an,int b1,int b2,int bn)
 64 {
 65     T=n+1;
 66     cnt=1;
 67     memset(head,0,sizeof(int)*(T+1));
 68     jia(0,a1,2*an);
 69     jia(a2,T,2*an);
 70     jia(0,b1,2*bn);
 71     jia(b2,T,2*bn);
 72     for(int i=1;i<=n;i++)
 73       for(int j=1;j<=n;j++)
 74         {
 75           if(ch[i][j]=='O')
 76             jia(i,j,2);
 77           if(ch[i][j]=='N')
 78             jia(i,j,inf);
 79         }
 80     return;
 81 }
 82 int main()
 83 {
 84     for(;scanf("%d%d%d%d%d%d%d",&n,&a1,&a2,&an,&b1,&b2,&bn)==7;)
 85     {
 86     a1++;
 87     a2++;
 88     b1++;
 89     b2++;
 90     for(int i=1;i<=n;i++)
 91       scanf("%s",ch[i]+1);
 92     build(a1,a2,an,b1,b2,bn);
 93     ans=0;
 94     for(;bfs();)
 95       ans+=dinic(0,inf);
 96     if(ans==2*an+2*bn)
 97       {
 98         build(a1,a2,an,b2,b1,bn);
 99         ans=0;
100         for(;bfs();)
101           ans+=dinic(0,inf);
102         if(ans==2*an+2*bn)
103           printf("Yes\n");
104         else
105           printf("No\n");
106       }
107     else
108       printf("No\n");}
109     return 0;
110 }

网络流。

posted on 2016-03-23 05:35  xiyuedong  阅读(...)  评论(...编辑  收藏