bool bfs(int x)
{
memset(mark,-1,sizeof(mark));
mark[x]=1;
int que[maxn];
int front=0,rear=0;
que[rear++]=x;
while(front!=rear)
{
int k=que[front++];front%=maxn;
for(int i=head[k];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(mark[v]==-1&&edge[i].flow>0)
{
mark[v]=mark[k]+1;
que[rear++]=v;
rear%=maxn;
if(v==des)return true;
}
}
}
return false;
}
int dfs(int u)
{
int Stack[maxn],cur[maxn];
memcpy(cur,head,sizeof(head));
int top=0;int res=0;
while(1)
{
if(u==des)
{
int min=INF,loc;
for(int i=0;i<top;i++){
if(min>edge[Stack[i]].flow){
min=edge[Stack[i]].flow;
loc=i;
}
}
for(int i=0;i<top;i++)
{
edge[Stack[i]].flow-=min;
edge[Stack[i]^1].flow+=min;
}
res+=min;
top=loc;
u=edge[Stack[top]].u;
}
for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)
{
if(edge[i].flow&&mark[u]+1==mark[edge[i].v])break;
}
if(cur[u]!=-1)
{
Stack[top++]=cur[u];
u=edge[cur[u]].v;
}
else
{
if(top==0)break;
mark[u]=-1;
u=edge[Stack[--top]].u;
}
}
return res;
}
int Dinic(int x)
{
int res=0;
while(bfs(src))res+=dfs(src);
return res;
}
网络流最大流之Dinic算法模板
最新推荐文章于 2024-09-18 01:00:38 发布