最小费用流 Bellman-Ford与Dijkstra 模板

本文介绍了最小费用流问题,提供了使用Bellman-Ford和Dijkstra算法的实现模板。针对含有负权边的情况,推荐使用Bellman-Ford算法,其时间复杂度为O(FEV),而没有负权边时,Dijkstra算法更为高效,复杂度为O(FElogV)。作者计划后续更新SPFA算法的理解和实现。
摘要由CSDN通过智能技术生成

今天刚接触最小费用流 码下两个版本当作模板用吧 spfa的做法明天再整理

有负权边的情况只能用Bellman-Ford 没有的话就用Dijkstra 毕竟Dijkstra效率更高



首先贴下Bellman-Ford 实现最小费用流的算法 时间复杂度为 O(FEV)   F E V 分别代表需要传输的流量 边的条数 节点的个数


// Bellman_Ford
//Bellman—Ford算法

//Bellman算法求最短增广路&最小费用流 O(FEV)
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<stack>
#include<queue>
#include<cmath>
#include<stack>
#include<list>
#include<map>
#include<set>
typedef long long ll;
using namespace std;
#define MV 11000 //顶点的最大个数
#define INF 0x3f3f3f3f

struct edge
{
    int t, cap, cost, rev;
    edge(int to = 0, int c = 0, int ct = 0, int r = 0): t(to), cap(c), cost(ct), rev(r) {};
};
vector <edge> G[MV];
int dis[MV];//the distance from source
int prevv[MV], preve[MV];//the previous node and 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值