The idea of a network flow diagram it's best to tidy up a wave of network flow building plans
1#include <bits/stdc++.h>2 using namespacestd;3 intN,h,t,a1,a2,an,b1,b2,bn,flow,now;Charch;4 intdis[ the],l[ the],d[ the][ the];Charc[ the][ the];5 Chargetch ()6 {7 for(Ch=getchar (); ch!='O'&& ch!='N'&& ch!='X'; ch=GetChar ());8 returnch;9 }Ten BOOLBFS () One { A for(intI=1; i<=n+1; i++) -dis[i]=0; - for(h=1, t=1, l[1]=0, dis[0]=1; h<=t;h++) the for(intI=1; i<=n+1; i++) - if(D[l[h]][i] &&!Dis[i]) -l[++t]=i,dis[i]=dis[l[h]]+1; - returndis[n+1]; + } - intDfsintNowintMaxflow) + { A if(now==n+1)returnMaxflow; at intflow=0; - for(intI=0; i<=n+1; i++) - if(dis[i]==dis[now]+1&&D[now][i]) - { - intThi=Dfs (I,min (maxflow,d[now][i)); -d[now][i]-=thi;d[i][now]+=Thi; inmaxflow-=thi;flow+=Thi; - if(maxflow==0)returnflow; to } + if(flow==0) -dis[now]=-1; the returnflow; * } $ voidINIT ()Panax Notoginseng { - for(intI=1; i<=n;i++) the for(intj=1; j<=n;j++) + Switch(C[i][j]) A { the Case'O':d [j][i]=d[i][j]=2; Break; + Case'N':d [J][i]=d[i][j]=flow; Break; - Case'X':d [j][i]=d[i][j]=0; Break; $ } $ for(intI=0; i<=n+1; i++) -d[0][i]=d[i][0]=d[i][n+1]=d[n+1][i]=0; -d[a1][0]=d[0][a1]=an<<1;d [b1][0]=d[0][b1]=bn<<1; thed[a2][n+1]=d[n+1][a2]=an<<1;d [b2][n+1]=d[n+1][b2]=bn<<1; -now=0;Wuyi } the intMain () - { Wu while(~SCANF ("%d%d%d%d%d%d%d",&n,&a1,&a2,&an,&b1,&b2,&bn)) - { Abouta1++;a2++;b1++;b2++; $flow=an+bn<<1; - for(intI=1; i<=n;i++) - for(intj=1; j<=n;j++) -c[i][j]=getch (); A INIT (); + while(Now<flow && BFS ()) Now=now+dfs (0, flow); the if(now<flow) - { $Puts"No"); the Continue; the } the swap (B1,B2); the INIT (); - while(Now<flow && BFS ()) Now=now+dfs (0, flow); in if(now<flow) thePuts"No"); the Else AboutPuts"Yes"); the } the return 0; the}
bzoj3504: [Cqoi2014] Dangerous bridge Network flow