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

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 的最小费用最大流(图会变为残量网络)

需要保证 first,second 在 Flow,Cost 范围内.
0 ≤ s , t < n 0\le s,t< n 0≤s,t<n

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

相关推荐
AAA修煤气灶刘哥2 小时前
AI 写的 SQL 跑通了,为什么和财务报表对不上?
人工智能·后端·算法
IvanCodes15 小时前
模型如何自我改进?拆解 RSI 的模型权重与 Harness 两条路线
人工智能·算法·ai·论文·agent·rsi
罗西的思考15 小时前
【轻量化复刻大模型 / KIMI K3】MXFP4 / MXFP8 量化感知训练(QAT)& 演示
人工智能·算法·机器学习
郝学胜-神的一滴18 小时前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
旖旎夜光19 小时前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
阿enna19 小时前
检测指标入门:IoU、Precision、Recall与mAP
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·cnn
Regulus-Moon19 小时前
Quorum:用一个不等式换掉分布式锁
数据结构·论文阅读·分布式·算法·系统架构
专注仿真20 小时前
仿真智能体训练:强化学习多目标奖励算法与实战
python·算法·强化学习·afsim智能体
老师我太想进步了202621 小时前
【数据结构-16】链式二叉树在线OJ题
c语言·数据结构·算法
LabVIEW开发21 小时前
UTM-30LX 激光雷达 + LabVIEW 扫描怎么采?
人工智能·算法·labview