最小费用最大流 + dijkstra 模版(处理负边)

 

概念

在同一个网络中,可能存在多个总流量相同的最大流,我们可以在计算流量的基础之上,给网络中的弧增加一个单位流量费用(简称费用),在确保流量最大的前提下总费用最小——最小费用最大流。

#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MAXN_ 5050
#define INF 0x3f3f3f3f
#define P pair<int,int>
using namespace std;
struct edge{ int to,cap,cost,rev;};
int n,m,flow,s,t,cap,res,cost,from,to,h[MAXN_];
std::vector<edge> G[MAXN_];
int dist[MAXN_],prevv[MAXN_],preve[MAXN_]; // 前驱节点和对应边
inline void add()
{
    G[from].push_back((edge){to,cap,cost,(int)G[to].size()});
    G[to].push_back((edge){from,0,-cost,(int)G[from].size()-1});
} // 在vector 之中找到边的位置所在!
inline int read()
{
    int x=0;
    char c=getchar();
    bool flag=0;
    while(c<'0'||c>'9'){if(c=='-')flag=1;    c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    return flag?-x:x;
}
inline void min_cost_flow(int s,int t,int f)
{
    fill(h+1,h+1+n,0);
    while(f > 0)
    {
        priority_queue<P,vector<P>, greater<P> > D;
        memset(dist,INF,sizeof dist);
        dist[s] = 0; D.push(P(0,s));
        while(!D.empty())
        {
            P now = D.top(); D.pop();
            if(dist[now.second] < now.first) continue;
            int v = now.second;
            for(int i=0;i<(int)G[v].size();++i)
            {
                edge &e = G[v][i];
                if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to])
                {
                    dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
                    prevv[e.to] = v;
                    preve[e.to] = i;
                    D.push(P(dist[e.to],e.to));
                }
            }
        }
        // 无法增广 , 就是找到了答案了!
        if(dist[t] == INF) break;
        for(int i=1;i<=n;++i) h[i] += dist[i]; 
        int d = f;
        for(int v = t; v != s; v = prevv[v])
            d = min(d,G[prevv[v]][preve[v]].cap);
        f -= d; flow += d;
        res += d * h[t];
        for(int v=t;v!=s;v=prevv[v])
        {
            edge &e = G[prevv[v]][preve[v]];
            e.cap -= d;
            G[v][e.rev].cap += d;
        }
    }
}
int main()
{
   n = read(); m = read(); s = read(); t = read();
   for(int i=1;i<=m;++i)
   {
           from = read(); to = read(); cap = read(); cost = read();
           add();
   }
   min_cost_flow(s,t,INF);
   printf("%d %d\n",flow,res);
   return 0;
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
最小费用最大流问题是图论中的一个重要问题,它是网络流问题的一种。该问题的目标是在给定的有向图中找到一条容量最大的流,使得流的总费用最小。如果图中存在负权回路,则该问题没有有限的解。 下面给出一个最小费用最大流负回路算法的 MATLAB 代码示例: ```matlab function [mf, mc] = min_cost_max_flow(n, s, t, cap, cost) % n: the number of nodes % s: the source node % t: the sink node % cap: the capacity matrix % cost: the cost matrix mf = 0; % the maximum flow mc = 0; % the minimum cost f = zeros(n); % the flow matrix d = inf(n, 1); % the shortest path distances p = zeros(n, 1);% the parent nodes while true % build the residual graph r = cap - f; [dist, prev] = dijkstra(n, s, t, r, cost); % if there is no path from s to t, terminate if dist(t) == inf break; end % calculate the bottleneck capacity b = inf; u = t; while u ~= s v = prev(u); b = min(b, r(v, u)); u = v; end % update the flow and cost u = t; while u ~= s v = prev(u); f(v, u) = f(v, u) + b; f(u, v) = f(u, v) - b; mc = mc + b * cost(v, u); u = v; end % update the maximum flow mf = mf + b; end function [dist, prev] = dijkstra(n, s, t, cap, cost) % dijkstra's algorithm with a heap dist = inf(n, 1); prev = zeros(n, 1); dist(s) = 0; q = [(1:n)', dist]; while ~isempty(q) [d, u] = min(q(:, 2)); if u == t break; end q(u, :) = []; for v = 1:n if cap(u, v) > 0 && d + cost(u, v) < dist(v) dist(v) = d + cost(u, v); prev(v) = u; q(q(:, 1) == v, 2) = dist(v); end end end ``` 下面是使用该函数求解一个具体的最小费用最大流问题的示例: ```matlab % the capacity matrix cap = [0 2 3 0 0; 0 0 0 3 4; 0 0 0 2 0; 0 0 0 0 2; 0 0 0 0 0]; % the cost matrix cost = [0 1 0 0 0; 0 0 0 1 0; 0 0 0 0 1; 0 0 0 0 1; 0 0 0 0 0]; % run the algorithm [mf, mc] = min_cost_max_flow(5, 1, 5, cap, cost); % print the results fprintf('maximum flow: %d\n', mf); fprintf('minimum cost: %d\n', mc); ``` 该问题的求解结果为: ``` maximum flow: 4 minimum cost: 7 ``` 这意味着在给定的有向图中,从节点1到节点5的最大流为4,且总费用为7。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wym_king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值