关于dinic的当前弧优化

P3376 【模板】网络最大流

sprads @ 2022-01-05 22:37:02

为什么这样

ll dinic(int x,ll In){
    if(x == t)return In;
    ll Bk = 0,res;
    for(int i = st[x];i && In;i = Next[i]){
        int y = ver[i];
        if(edge[i] && d[y] == d[x] + 1){
            res = dinic(y,min(In,edge[i]));
            In -= res,Bk += res;
            edge[i] -= res,edge[i ^ 1] += res;
        }
        st[x] = Next[i];//here
    }
    if(Bk == 0)
        d[x] = 0;
    return Bk;
}

如此之慢 提交记录

而这样

ll dinic(int x,ll In){
    if(x == t)return In;
    ll Bk = 0,res;
    for(int i = st[x];i && In;i = Next[i]){
        int y = ver[i];
        if(edge[i] && d[y] == d[x] + 1){
            res = dinic(y,min(In,edge[i]));
            In -= res,Bk += res;
            edge[i] -= res,edge[i ^ 1] += res;
        }
        if(!edge[i])st[x] = Next[i];//here
    }
    if(Bk == 0)
        d[x] = 0;
    return Bk;
}

却快了不少提交记录


by Celtic @ 2022-01-05 22:50:33

因为上面的那个会多跑一条边


by Celtic @ 2022-01-05 22:51:01

你考虑如果你剩下的流量不够跑完 i 这条边


by Celtic @ 2022-01-05 22:51:16

那你下次还是得从 i 开始


by Celtic @ 2022-01-05 22:51:56

但是上面这样写 i 就得在下一次 bfs 之后才会接着跑


by Celtic @ 2022-01-05 22:52:23

上面那个复杂度不对应该


by sprads @ 2022-01-05 22:57:01

@Imitatоrs 谢谢帮助,弄懂了


|