最小费用最大流MCMF算法(模板)

计算最小费用最大流,网络中可以有负边,但不能存在负权圈。如果要固定流量k,可以在增广的时候检查一下,在flow+a>=k的时候只增广k-flow单位的流量,然后终止程序

#include<bits/stdc++.h>
using namespace std;

const int inf=2e9;
const int maxn=10050;

struct Edge{
    int from,to,cap,flow,cost;
    Edge(int u,int v,int c,int f,int co):from(u),to(v),cap(c),flow(f),cost(co){}
};

struct MCMF{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> g[maxn];
    int inq[maxn];  
    int d[maxn];    
    int p[maxn];    
    int a[maxn];    

    void init(int n){
        this->n=n;
        for(int i=0;i<n;++i) g[i].clear();
        edges.clear();
    }

    void add(int from,int to,int cap,int cost){
        edges.push_back(Edge(from,to,cap,0,cost));
        edges.push_back(Edge(to,from,0,0,-cost));
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }

    bool BellmanFord(int s,int t,int& flow,long long& cost){
        for(int i=0;i<n;++i) d[i]=inf;
        memset(inq,0,sizeof(inq));
        d[s]=0;
        inq[s]=1;
        p[s]=0;
        a[s]=inf;

        queue<int> que;
        que.push(s);
        while(!que.empty()){
            int u=que.front();
            que.pop();
            inq[u]=0;
            for(int i=0;i<g[u].size();++i){
                Edge& e=edges[g[u][i]];
                if(e.cap>e.flow && d[e.to]>d[u]+e.cost){
                    d[e.to]=d[u]+e.cost;
                    p[e.to]=g[u][i];
                    a[e.to]=min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){ que.push(e.to);inq[e.to]=1; }
                }
            }
        }
        if(d[t]==inf) return false;
        flow+=a[t];
        cost+=(long long)d[t]*(long long)a[t];
        for(int u=t;u!=s;u=edges[p[u]].from){
            edges[p[u]].flow+=a[t];
            edges[p[u]^1].flow-=a[t];
        }
        return true;
    }

    int MincostMaxflow(int s,int t,long long& cost){
        int flow=0;
        cost=0;
        while(BellmanFord(s,t,flow,cost));
        return flow;
    }
};
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java实现最小费用最大流的示例代码,使用了Edmonds-Karp算法和SPFA算法: ```java import java.util.Arrays; import java.util.LinkedList; public class MinCostMaxFlow { private int n, m, s, t, maxFlow, minCost; private int[] head, dis, pre, flow, vis; private Edge[] edges; private LinkedList<Integer> queue; public MinCostMaxFlow(int n, int m, int s, int t) { this.n = n; this.m = m; this.s = s; this.t = t; this.head = new int[n + 1]; this.edges = new Edge[m + 1]; this.dis = new int[n + 1]; this.pre = new int[n + 1]; this.flow = new int[n + 1]; this.vis = new int[n + 1]; this.queue = new LinkedList<>(); Arrays.fill(head, -1); } public void addEdge(int u, int v, int cap, int cost) { edges[m] = new Edge(u, v, cap, cost, head[u]); head[u] = m++; edges[m] = new Edge(v, u, 0, -cost, head[v]); head[v] = m++; } private boolean spfa() { Arrays.fill(dis, Integer.MAX_VALUE); Arrays.fill(vis, 0); dis[s] = 0; vis[s] = 1; flow[s] = Integer.MAX_VALUE; queue.add(s); while (!queue.isEmpty()) { int u = queue.poll(); vis[u] = 0; for (int i = head[u]; i != -1; i = edges[i].next) { int v = edges[i].v; if (edges[i].cap > 0 && dis[v] > dis[u] + edges[i].cost) { dis[v] = dis[u] + edges[i].cost; pre[v] = i; flow[v] = Math.min(flow[u], edges[i].cap); if (vis[v] == 0) { vis[v] = 1; queue.add(v); } } } } return dis[t] != Integer.MAX_VALUE; } public void minCostMaxFlow() { while (spfa()) { int u = t; maxFlow += flow[t]; minCost += dis[t] * flow[t]; while (u != s) { edges[pre[u]].cap -= flow[t]; edges[pre[u] ^ 1].cap += flow[t]; u = edges[pre[u]].u; } } } static class Edge { int u, v, cap, cost, next; public Edge(int u, int v, int cap, int cost, int next) { this.u = u; this.v = v; this.cap = cap; this.cost = cost; this.next = next; } } } ``` 使用方法如下: ```java // 创建一个最小费用最大流对象 MinCostMaxFlow mcmf = new MinCostMaxFlow(n, m, s, t); // 添加边 mcmf.addEdge(u, v, cap, cost); // 求解最小费用最大流 mcmf.minCostMaxFlow(); // 最大流量 int maxFlow = mcmf.maxFlow; // 最小费用 int minCost = mcmf.minCost; ``` 其中,`n`表示点的数量,`m`表示边的数量,`s`表示源点,`t`表示汇点,`u`、`v`、`cap`、`cost`分别表示边的起点、终点、容量和费用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值