最小费用流bellman-ford算法总结

``````#include <vector>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

const int MAX_V = 1005;
const int INF = 0x3f3f3f3f;
struct Edge{
int to,cap,cost,rev;
Edge(int t,int c,int co,int r):to(t),cap(c),cost(co),rev(r) {}
};
vector<Edge> G[MAX_V];
int V;
int prevv[MAX_V],preve[MAX_V];
int dist[MAX_V];

void add_edge(int from,int to,int cap,int cost){
G[from].push_back( Edge(to,cap,cost,G[to].size()) );
G[to].push_back( Edge(from,0,-cost,G[from].size()-1) );
}

int min_cost_flow(int s,int t,int f){
int res = 0;
while(f>0){
fill(dist,dist+V,INF);
dist[s] = 0;
bool update = true;
while(update){
for(int v=0; v<V; v++){
if(dist[v] == INF) continue;
for(int i=0; i<G[v].size(); i++){
Edge &e = G[v][i];
if(e.cap>0 && dist[e.to] > dist[v] + e.cost){
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if(dist[t] == INF){
return -1;
}
int d = f;
for(int v = t; v != s; v = prevv[v]){
d = min( d , G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * dist[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;
}
}
return res;
}

int main()
{
return 0;
}
``````

原文作者：Bellman - ford算法
原文地址: https://blog.csdn.net/liuzhan214/article/details/52071366
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。