费用流消圈定理

有负权环的费用流直接跑EK会死循环,根据消圈定理:该最小费用流最后的残余网络不存在负权环。

可以提前消去负权环,

复制代码
bool clear_circle(int &cost){
    memset(cnt,0,sizeof cnt);
    memset(d,0x3f,sizeof d);
    memset(vis,0,sizeof vis);
    memset(mark,0,sizeof mark);
    int hh = 0,tt = 1;
    q[0] = S,d[S] = 0;
    while(hh!=tt){
        int u = q[hh++];
        if(hh == N)hh = 0;
        vis[u] = false;
        for(int i = h[u];~i;i=ne[i]){
            int j = e[i];
            if(f[i] && d[j]>d[u]+w[i]){
                cnt[j] = cnt[u] + 1;
                d[j] = d[u] + w[i];
                pre[j] = i;
                if(cnt[j]>=n+2){
                    
                    int t = j;
                    while(!mark[t]){
                        mark[t] = true;
                        t = e[pre[t]^1];
                    }
                    bool flg = true;
                    int mx = 1e9;
                    for(int i = t;flg || i!=t;i=e[pre[i]^1]){
                        flg = false;
                        // cout<<i<<" "<<e[pre[i]^1]<<" "<<w[i]<<'\n';
                        mx = min(mx,f[pre[i]]);
                        // f[pre[i]]-=1;
                        // f[pre[i]^1]+=1;
                        // cost += w[pre[i]];
                    }
                    flg = true;
                    for(int i = t;flg || i!=t;i=e[pre[i]^1]){
                        flg = false;
                        // cout<<i<<" "<<e[pre[i]^1]<<" "<<w[i]<<'\n';
                        f[pre[i]]-=mx;
                        f[pre[i]^1]+=mx;
                        cost += mx*w[pre[i]];
                    }
                    return true;
                }
                if(!vis[j]){
                    q[tt++] = j;
                    if(tt == N)tt = 0;
                    vis[j] = true;
                }
            }
        }
    }
    return false;
}
View Code
复制代码

详细如上

posted @   wanghai673  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示