之前写网络流dinic没有当前弧优化,
而且还会多开一个变量p(辅助bfs)
在bfs时使用stl中的queue
这次的模板改变了以上的所有弊端,
全新的写法,撒花
这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int INF=0x33333333;
const int N=100001;
struct node{
int x,y,nxt,v;
};
node way[N<<1];
int heap[N],tou,wei,s,t,st[N],tot=-1;
int deep[N],cur[N],n;
int bfs(int s,int t)
{
tou=0;
wei=1;
heap[wei]=s;
for (int i=1;i<=n;i++) cur[i]=st[i]; //每个点的st复制一遍
memset(deep,-1,sizeof(deep));
deep[s]=1;
do
{
tou++;
int r=heap[tou];
for (int i=st[r];i!=-1;i=way[i].nxt)
{
int y=way[i].y;
if (way[i].v&&deep[y]==-1)
{
deep[y]=deep[r]+1;
wei++;
heap[wei]=y;
}
}
}while (tou<wei);
return deep[t]!=-1;
}
int dfs(int now,int t,int limit)
{
if (!limit||now==t) return limit;
int f,flow=0;
for (int i=cur[now];i!=-1;i=way[i].nxt)
{ //循环cur
cur[now]=i;
if (way[i].v&&deep[way[i].y]==deep[now]+1&&(f=dfs(way[i].y,t,min(limit,way[i].v))))
{
way[i].v-=f;
way[i^1].v+=f;
limit-=f;
flow+=f;
if (!limit) break;
}
}
return flow;
}
int doit()
{
int ans=0;
while (bfs(s,t))
ans+=dfs(s,t,INF);
return ans;
}
int main()
{
memset(st,-1,sizeof(st));
//lianbian(); 建图
doit();
}