就是个板子
最大流Dinic
bool bfs(int s,int en) { queue<int> q; memset(deep,0,sizeof(deep)); q.push(s); deep[s]=1; do{ int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=e[i].next) { if(e[i].flow>0 && deep[e[i].to]==0) { q.push(e[i].to); deep[e[i].to]=deep[u]+1; } } }while(!q.empty()); if(deep[en]==0) return false; else return true; } int dfs(int x,int dist,int y) { if(x==y) return dist; for(int i=head[x];i!=-1;i=e[i].next) { if(deep[e[i].to]==deep[x]+1 && e[i].flow!=0) { int di=dfs(e[i].to,min(dist,e[i].flow),y); if(di>0) { e[i].flow-=di; e[i^1].flow+=di; return di; } } } return 0; } int dinic(int s,int e) { int ans=0; while(bfs(s,e)) { while(int k=dfs(s,inf,e)) ans+=k; } return ans; }
最小费用最大流
bool spfa(int x,int y) { memset(vis,0,sizeof(vis)); for(int i=0;i<=n+1;i++) dis[i]=inf; dis[x]=0; q.push(x); vis[x]=1; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=e[i].next) { if(dis[u]+e[i].cost<dis[e[i].to] && e[i].flow>0) { dis[e[i].to]=dis[u]+e[i].cost; pe[e[i].to]=i; pv[e[i].to]=u; if(!vis[e[i].to]) { q.push(e[i].to); vis[e[i].to]=1; } } } } return dis[y]<inf; } void max_flow(int x,int y) { int kflow; while(spfa(x,y)) { printf("%d %d ",ansf,ansc); kflow=inf; for(int i=y;i!=x;i=pv[i]) { kflow=min(kflow,e[pe[i]].flow); } ansf+=kflow; ansc+=kflow*dis[y]; for(int i=y;i!=x;i=pv[i]) { e[pe[i]].flow-=kflow; e[pe[i]^1].flow+=kflow; } } }