- Dinic算法模板
namespace Dinic{ int s,t; int d[1010],cur[1010]; bool bfs(){ for(int i=0;i<1010;i++) d[i]=-1; d[s]=0; cur[s]=head[s]; queue<int> q; q.push(s); while(!q.empty()){ int x=q.front(); q.pop(); for(int i=head[x];i!=-1;i=edge[i].next){ int y=edge[i].to,w=edge[i].w; if(w&&d[y]==-1){ d[y]=d[x]+1; cur[y]=head[y]; q.push(y); if(y==t) return true; } } } return false; } int dfs(int p=s,int flow=0x3f3f3f3f){ if(p==t) return flow; int sum=flow; for(int i=cur[p];i!=-1&∑i=edge[i].next){ cur[p]=i; int y=edge[i].to,w=edge[i].w; if(w&&d[y]==d[p]+1){ int c=dfs(y,min(w,sum)); edge[i].w-=c; sum-=c; edge[i^1].w+=c; } } return flow-sum; } int dinic(){ int ans=0; while(bfs()){ ans+=dfs(); } return ans; } }
网络流模板
最新推荐文章于 2022-02-20 16:11:30 发布