再谈最小费用最大流算法
来源:互联网 发布:昆山美工培训多少钱 编辑:程序博客网 时间:2024/06/04 18:00
我在之前的博客中讲过最小费用最大流算法,但是实现的时候使用的是邻接矩阵,无法支持多重边。下面我们仍然实现这个算法,但采用的是新的数据结构,代码如下
struct Edge {int from, to, cap, flow, cost;};int n,m,s,t;vector<Edge> edges;vector<int> G[maxn];int inq[maxn]; //是否在队列中int d[maxn]; //Bellman-Fordint p[maxn]; //上一条弧int a[maxn]; //可改进量void AddEdge (int from, int to, int cap, int cost){edges.push_back((Edge){from,to,cap,0,cost});edges.push_back((Edge){from,to,0,0,-cost});m = edges.size();G[from].push_back(m-2);G[to].push_back(m-1);}int flow,cost;bool Bellford (){queue<int> q;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;q.push(s);while(!q.empty()){int u = q.front(), q.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] = (a[u]>e.cap-e.flow ? e.cap-e.flow : a[u])if(!inq[e.to]){q.push(e.to); inq[e.to] = 1;}}}}if(d[t]==INF) return false;for(int i = t; i!=s; i = edges[p[i]].from){edges[p[i]].flow += a[t];edges[p[i]^1].flow -= a[t];}flow += a[t];cost += a[t]*d[t];return true;} //主过程: int main (){ flow = 0, cost = 0; while(Bellford()); return cost; }
0 0
- 再谈最小费用最大流算法
- 最小费用最大流算法
- 最小费用最大流算法
- 最小费用最大流算法
- 最小费用最大流算法
- 最小费用最大流算法
- 最小费用最大流;最小费用路算法;
- 最小费用最大流 zkw算法
- 最小费用最大流MCMF算法
- 最小费用最大流的另类算法
- 最小费用最大流
- 最小费用最大流
- 最小费用最大流
- 最小费用最大流
- 【最小费用最大流】
- 最小费用最大流
- 最小费用最大流
- 最小费用最大流
- js验证码
- java.io.Serializable浅析
- JQUERY学习日记
- SQL Server 2008 R2 导出数据脚本的方法(含数据)
- setsid函数说明
- 再谈最小费用最大流算法
- 原始js拼接json
- php上传文件大小限制
- ckeditor增加行距功能和中文字体
- JQuery 判断起始日期与结束日期大小
- DB2: Load
- PHP Memcache扩展
- 115_leetcode_Permutation Sequence
- PHP日期相减