[USACO5.4] cow's telecom telecowmunication
Ideas:
Water problem;
Code:
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#defineMAXN 205#defineMAXM 20005#defineINF 0x3f3f3f3fintHead[maxn],cnt=1, n,m,e[maxm],v[maxm],f[maxm],s,t;intque[maxm<<2],deep[maxn],c1,c2;inlinevoid inch(int&Now ) { CharCget=getchar (); now=0; while(cget>'9'|| cget<'0') cget=GetChar (); while(cget>='0'&&cget<='9') { now=now*Ten+cget-'0'; Cget=GetChar (); }}inlinevoidEdge_add (intUintVintf) {e[++cnt]=head[u],v[cnt]=v,f[cnt]=f,head[u]=CNT; e[++cnt]=head[v],v[cnt]=u,f[cnt]=0, head[v]=CNT;}BOOLBFs () {memset (deep,-1,sizeof(deep)); intH=0, tail=1, now;que[0]=s,deep[s]=0; while(h<tail) { Now=que[h++]; for(intI=head[now];i;i=E[i]) { if(deep[v[i]]<0&&F[i]) {Deep[v[i]]=deep[now]+1; if(v[i]==t)return true; Que[tail++]=V[i]; } } } return false;}intFlowing (intNowintflow) { if(t==now| | flow<=0)returnflow; intoldflow=0, POS; for(intI=head[now];i;i=E[i]) { if(! f[i]| | deep[v[i]]!=deep[now]+1)Continue; POS=Flowing (V[i],min (flow,f[i)); Flow-=pos,oldflow+=pos,f[i]-=pos,f[i^1]+=POS; if(!flow)returnOldflow; } if(!oldflow) deep[now]=-1; returnOldflow;}intMain () {inch(n),inch(m),inch(C1),inch(C2), s=c1+n,t=C2; for(intI=1; i<=n;i++) Edge_add (I,i+n,1); while(m--) { inch(C1),inch(C2); Edge_add (C1+N,c2,inf); Edge_add (C2+N,c1,inf); } intans=0; while(BFS ()) ans+=flowing (S,inf); cout<<ans; return 0;}
AC diary--[usaco5.4] cow's telecom telecowmunication Rokua P1345