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 38 39 40 41 42 43 44 45 46 47
| int head[N],p=1,S,T,n,m,k; struct ss{int v,last;int rl,w;}G[M<<1]; void add(int a,int b,int c,int d){ G[++p]=(ss){b,head[a],c,d};head[a]=p; G[++p]=(ss){a,head[b],0,-d};head[b]=p; } queue<int> Q; int dis[N],Mincost,Maxflow; bool used[N]; bool vis[N]; bool SPFA(int S,int T){ for(int i=0;i<N;i++)dis[i]=inf,vis[i]=0; dis[T]=0,vis[T]=1; Q.push(T); while(!Q.empty()){ int u=Q.front();Q.pop();vis[u]=0; for(int i=head[u];i;i=G[i].last) if(G[i^1].rl>0&&dis[G[i].v]>dis[u]-G[i].w){ dis[G[i].v]=dis[u]-G[i].w; if(!vis[G[i].v]) Q.push(G[i].v),vis[G[i].v]=1; } } return dis[S]<inf; } int Augment(int a,int S,int T,int mi){ used[a]=1; if(a==T)return mi; int flow=0; for(int i=head[a];i;i=G[i].last) if(!used[G[i].v]&&G[i].rl&&dis[a]-G[i].w==dis[G[i].v]){ int ls=Augment(G[i].v,S,T,min(G[i].rl,mi)); Mincost+=ls*G[i].w,mi-=flow,G[i].rl-=ls,G[i^1].rl+=ls,flow+=ls; if(!mi)return flow; } return flow; } void ZKW(int S,int T){ Maxflow=Mincost=0; while(SPFA(S,T)){ used[T]=1; while(used[T]){ memset(used,0,sizeof(used)); Maxflow+=Augment(S,S,T,inf); } } }
|