網路流之 最小費用最大流

2021-07-02 11:36:15 字數 902 閱讀 2247

學習最小費用最大流前,需要學習最大流演算法。在最大流演算法中,沒有考慮邊的費用問題。在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的最小費用道路為止。

最小費用最大流演算法還可以解決二分圖最優匹配。

最小費用最大流模板:

cpp**  

const

int size = 1102;  

const

int inf = 0x7fffffff;  

struct edge  

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)  

bool spfa(int s, int t)  

}  }  }  

return pre[t] != -1;  

}  int mincostflow(int s, int t, int flow)  

}  return cost; // flow是最大流值

}  

參見poj2516 2195 2135。

網路流 最小費用最大流

q 為什麼突然想搞網路流?a 迫於tham 蛤mu的淫威 用最短路演算法求出s t的路徑 把路徑要摳出來,而且每條邊要有容量 算一下路徑裡面的可以流過的最大的流量 發現此時的花費就是 dis t flow 累加即可.重複1 3直到不能夠到達t.include include include incl...

網路流之最小費用流

這是一道模板題。給定乙個圖,每條邊有容量和費用,使用每條邊的單位流量需要支付特定的費用。給定源點 1和匯點 n,求圖的最大流和最大流需要支付的最小費用。第一行兩個整數n,m,表示有 n 個點 m 條邊。從第二行開始的之後 m 行,每行四個整數s i t i c i w i 表示一條從 s i 到 t...

網路流 最大流 最小割 最小費用流

sap gap 鄰接表 sap gap 鄰接矩陣 dinic 鄰接矩陣 dinic 鄰接表 介個人寫的敲詳細 include include include include include using namespace std const int maxn 5010 const int maxn ...