生活随笔
收集整理的這篇文章主要介紹了
网络流之——最小费用最大流
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ?學習最小費用最大流前,需要學習最大流算法。在最大流算法中,沒有考慮邊的費用問題。在MinCostMaxFlow中,引入了費用的概念:cij表示邊(i,j)單位流量的費用。在滿足流量=v(f)的同時,并且要求費用最少。
??? 最小費用最大流的迭代算法:
??? 1.求出從s到t的最小費用通路(spfa)和通路的最大流量f。
??? 2.讓通路上的邊(i,j)流量減去f;添加反向邊(j,i),容量為f,費用為-cost(i,j)。
??? 3.重復1,2,直到從s到t的流量=v(f)或者再也找不到從s到t的最小費用道路為止。
最小費用最大流算法還可以解決二分圖最優(yōu)匹配。
?
最小費用最大流模板:
Cpp代碼??
const?int?size?=?1102;?? const?int?INF?=?0x7fffffff;?? struct?Edge?? {?? ????int?to;?? ????int?vol;?? ????int?cost;?? ????int?next;?? }e[size*40];?? int?index[size];?? int?edgeNum;?? int?pre[size],?pos[size];?? int?dis[size],?que[size*10];?? bool?vis[size];?? ?? void?insert(int?from,?int?to,?int?vol,?int?cost)?? {?? ????e[edgeNum].to?=?to;?? ????e[edgeNum].vol?=?vol;?? ????e[edgeNum].cost?=?cost;?? ????e[edgeNum].next?=?index[from];?? ????index[from]?=?edgeNum++;?? ????e[edgeNum].to?=?from;?? ????e[edgeNum].vol?=?0;?? ????e[edgeNum].cost?=?-cost;?? ????e[edgeNum].next?=?index[to];?? ????index[to]?=?edgeNum++;?? }?? ?? bool?spfa(int?s,?int?t)?? {?? ????int?i;?? ????memset(pre,?-1,?sizeof(pre));?? ????memset(vis,?0,?sizeof(vis));?? ????int?head,?tail;?head?=?tail?=?0;?? ????for(i?=?0;?i?<?size;?i++)?? ????????dis[i]?=?INF;?? ????que[tail++]?=?s;?? ????pre[s]?=?s;?? ????dis[s]?=?0;?? ????vis[s]?=?1;?? ????while(head?!=?tail)?? ????{?? ????????int?now?=?que[head++];?? ????????vis[now]?=?0;?? ????????for(i?=?index[now];?i?!=?-1;?i?=?e[i].next)?? ????????{?? ????????????int?adj?=?e[i].to;?? ????????????if(e[i].vol?>?0?&&?dis[now]?+?e[i].cost?<?dis[adj])?? ????????????{?? ????????????????dis[adj]?=?dis[now]?+?e[i].cost;?? ????????????????pre[adj]?=?now;?? ????????????????pos[adj]?=?i;?? ????????????????if(!vis[adj])?? ????????????????{?? ????????????????????vis[adj]?=?1;?? ????????????????????que[tail++]?=?adj;?? ????????????????}?? ????????????}?? ????????}?? ????}?? ????return?pre[t]?!=?-1;?? }?? ?? int?MinCostFlow(int?s,?int?t,?int?flow)?? {?? ????int?i;?? ????int?cost?=?0;?? ????flow?=?0;?? ????while(spfa(s,?t))?? ????{?? ????????int?f?=?INF;?? ????????for(i?=?t;?i?!=?s;?i?=?pre[i])?? ????????????if?(e[pos[i]].vol?<?f)?f?=?e[pos[i]].vol;?? ????????flow?+=?f;?cost?+=?dis[t]?*?f;?? ????????for(i?=?t;?i?!=?s;?i?=?pre[i])?? ????????{?? ????????????e[pos[i]].vol?-=?f;?? ????????????e[pos[i]?^?1].vol?+=?f;?? ????????}?? ????}?? ????return?cost;??? }??
?
參見poj2516 2195 2135。
總結(jié)
以上是生活随笔為你收集整理的网络流之——最小费用最大流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。