1#include <cstdio>2#include <iostream>3#include <cstring>4 #defineM 1000095 #defineINF 21390621436 using namespacestd;7 intN,a1,a2,an,b1,b2,bn,tot,cnt=1, t,ans,head[m],d[m],q[2*m],next[Ten*m],u[Ten*m],v[Ten*M];8 Charch[ -][ -];9 BOOLBFS ()Ten { Onememset (D,0,sizeof(int) * (t+1)); A intH=0, t=1; -q[1]=0; -d[0]=1; the for(;h<t;) - { -h++; - intp=Q[h]; + for(intI=head[p];i;i=Next[i]) - if(!d[u[i]]&&V[i]) + { Ad[u[i]]=d[p]+1; at if(D[t]) - return 1; -t++; -q[t]=U[i]; - } - } in return 0; - } to intDinic (intSintf) + { - if(s==T) the returnF; * intrest=F; $ for(intI=head[s];i&&rest;i=Next[i])Panax Notoginseng if(v[i]&&d[u[i]]==d[s]+1) - { the intnow=dinic (U[i],min (rest,v[i)); + if(!Now ) Ad[u[i]]=0; thev[i]-=Now ; +v[i^1]+=Now ; -rest-=Now ; $ } $ returnF-rest; - } - voidJia1 (intA1,intA2,intA3) the { -cnt++;Wuyinext[cnt]=HEAD[A1]; thehead[a1]=CNT; -u[cnt]=A2; Wuv[cnt]=A3; - return; About } $ voidJiaintA1,intA2,intA3) - { - jia1 (A1,A2,A3); -Jia1 (A2,A1,0); A return; + } the voidBuildintA1,intA2,intAnintB1,intB2,intbn) - { $t=n+1; theCnt=1; theMemset (Head,0,sizeof(int) * (t+1)); theJia0, A1,2*an ); theJia (A2,t,2*an ); -Jia0, B1,2*bn); inJia (B2,t,2*bn); the for(intI=1; i<=n;i++) the for(intj=1; j<=n;j++) About { the if(ch[i][j]=='O') theJia (I,j,2); the if(ch[i][j]=='N') + Jia (i,j,inf); - } the return;Bayi } the intMain () the { - for(; scanf ("%d%d%d%d%d%d%d", &n,&a1,&a2,&an,&b1,&b2,&bn) = =7;) - { thea1++; thea2++; theb1++; theb2++; - for(intI=1; i<=n;i++) thescanf"%s", ch[i]+1); the build (a1,a2,an,b1,b2,bn); theans=0;94 for(; BFS ();) theAns+=dinic (0, INF); the if(ans==2*an+2*bn) the {98 build (a1,a2,an,b2,b1,bn); Aboutans=0; - for(; BFS ();)101Ans+=dinic (0, INF);102 if(ans==2*an+2*bn)103printf"yes\n");104 Else theprintf"no\n");106 }107 Else108printf"no\n");}109 return 0; the}
Network flow.
Bzoj 3504: [Cqoi2014] Dangerous bridge