1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| bool GetLevel() { memset(Level,-1,sizeof(Level)); int head=0,tail=1; Queue[1]=1; Level[1]=0; while(++head<=tail) { for(int i=Link[Queue[head]];i;i=a[i].Next) if(Level[a[i].Point]<0&&a[i].Value) { Queue[++tail]=a[i].Point; Level[Queue[tail]]=Level[Queue[head]]+1; } } return Level[n]>=0; } int GetMaxFlow(int x,int Flow) { if(x==n) return Flow; int MaxFlow=0,D=0; for(int i=Link[x];i&&MaxFlow<Flow;i=a[i].Next) if(a[i].Value&&Level[a[i].Point]==Level[x]+1) if(D=GetMaxFlow(a[i].Point,min(Flow-MaxFlow,a[i].Value))) { MaxFlow+=D; a[i].Value-=D; a[a[i].Back].Value+=D; } if(!MaxFlow) Level[x]=-1; return MaxFlow; } void Dinic() { int ans; while(GetLevel()) while(ans=GetMaxFlow(1,123456789)) all+=ans; }
|