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

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 一边加弧.

相关推荐
码匠许师傅14 小时前
【C++ 面试真题】C++ 的 const 和 constexpr 有什么区别?
c++·面试
坚持编程的菜鸟16 小时前
模拟实现memmove
c语言·算法·模拟实现memmove
wabs66617 小时前
关于图论【最短路径之Dijkstra算法(堆优化版)|卡码网47.参加科学大会的思考】
数据结构·算法·图论·优先级队列·邻接表·小顶堆·卡码网
果果燕17 小时前
实习笔记(一):NFS、Samba、VNC、Git、CMake、systemd、内核、交叉编译
linux·arm开发·c++·笔记·git
坚持编程的菜鸟17 小时前
编写判断大小端程序
c语言·算法·判断大小端
papaofdoudou17 小时前
判断排列逆序奇偶性的乘积判别法(范德蒙德符号法)
人工智能·算法
牛艺翔18 小时前
C++基础
开发语言·c++
键盘会跳舞18 小时前
C++ :容器适配器stack源码级拆解
开发语言·c++··stack·先进后出
linux-hzh18 小时前
百日算法修炼 · Day 03
java·算法
问商十三载18 小时前
RAG 检索效果差怎么排查?2026 五层诊断法完整指南
开发语言·人工智能·windows·python·算法