模板分享:网络最小费用流

Code

改自 jiangly 的模板,使用 Primal-Dual 算法.

cpp 复制代码
template<class Flow, class Cost>
struct MCFGraph {
    struct Edge {
        int v;
        Flow c;
        Cost f;
        Edge(int v, Flow c, Cost f) : v(v), c(c), f(f) {}
    };
    
    const int n;
    std::vector<Edge> e;
    std::vector<std::vector<int>> g;
    std::vector<Cost> h, dis;
    std::vector<int> pre;
    
    bool dijkstra(int s, int t) {
        dis.assign(n, std::numeric_limits<Cost>::max());
        pre.assign(n, -1);
        
        std::priority_queue<
            std::pair<Cost, int>, 
            std::vector<std::pair<Cost, int>>, 
            std::greater<std::pair<Cost, int>>
        > que;
        
        dis[s] = 0;
        que.emplace(0, s);
        
        while (!que.empty()) {
            Cost d = que.top().first;
            int u = que.top().second;
            que.pop();
            
            if (dis[u] < d) continue;
            for (int i : g[u]) {
                int v = e[i].v;
                Flow c = e[i].c;
                Cost f = e[i].f;
                if (c > 0 && dis[v] > d + h[u] - h[v] + f) {
                    dis[v] = d + h[u] - h[v] + f;
                    pre[v] = i;
                    que.emplace(dis[v], v);
                }
            }
        }
        return dis[t] != std::numeric_limits<Cost>::max();
    }

    MCFGraph(int n) : n(n), g(n) {}
    void addEdge(int u, int v, Flow c, Cost f) {
	    g[u].push_back(e.size());
	    e.emplace_back(v, c, f);
	    g[v].push_back(e.size());
	    e.emplace_back(u, 0, -f);
	}

    std::pair<Flow, Cost> flow(int s, int t) {
        Flow flow = 0;
        Cost cost = 0;
        h.assign(n, 0);
        
        while (dijkstra(s, t)) {
            for (int i = 0; i < n; ++i) h[i] += dis[i];
            Flow aug = std::numeric_limits<Flow>::max();
            for (int i = t; i != s; i = e[pre[i] ^ 1].v) aug = std::min(aug, e[pre[i]].c);
            for (int i = t; i != s; i = e[pre[i] ^ 1].v) {
                e[pre[i]].c -= aug;
                e[pre[i] ^ 1].c += aug;
            }
            flow += aug;
            cost += h[t] * aug;
        }
        return std::make_pair(flow, cost);
    }
};

Usage

cpp 复制代码
MCFGraph<Flow, Cost>::MCFGraph(int n);

创建一个网络图,有 n n n 个点,没有弧,容量类型为 Flow,费用类型为 Cost(只支持整数)

cpp 复制代码
void MCFGraph<Flow, Cost>::addEdge(int u, int v, Flow c, Cost f);

在图中加入一条 u → v u\to v u→v,流量为 c c c ,单位流量费用为 f f f 的有向弧.
0 ≤ u , v < n 0\le u,v< n 0≤u,v<n, c c c 不超过 Flow 的范围, f f f 不超过 Cost 的范围.

cpp 复制代码
pair<Flow, Cost> MCFGraph<Flow, Cost>::flow(int s, int t);

求出 s s s 到 t t t 的最小费用最大流(图会变为残量网络)

需要保证 firstsecondFlowCost 范围内.
0 ≤ s , t < n 0\le s,t< n 0≤s,t<n

注意:不支持一边跑 flow 一边加弧.

相关推荐
JieE2121 天前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
JieE2122 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack203 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树3 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2123 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2123 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术4 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦4 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
clint4564 天前
C++进阶(1)——前景提要
c++