網路流 最小費用最大流問題

2021-10-07 23:44:05 字數 2657 閱讀 5921

前置知識:最大流問題

最小費用最大流問題:在最大流問題基礎上,為每條邊賦值單位流量的花費。求解保證最大流時,最小花費為多少。(因為最大流可以有多種流分配方案)

以ek演算法為基礎,在bfs時增加求最短路即可(單位流量花費作為最短路權值)。每次尋找增廣路都要找一次最短路,找到後,該增廣路的最小花費為流量*最短路,然後把所有的增廣路的最小花費求和即為總的最小花費。

最大費用只需建圖時費用取反,跑最小費用流,結果取反即可。

訪問邊時有兩種實現,鄰接矩陣和鏈式前向星,鏈式前向星較複雜,但空間占用低。當節點數n>5000時,應考慮鏈式前向星做法。

後續內容是針對有向圖的,無向圖需要特殊處理:(參考無向圖的最大流與費用流)

例題:p3381 【模板】最小費用最大流

1.鄰接矩陣做法:(例題mle3個點)

//ek,鄰接矩陣

#include

#define

maxn

5001

using

namespace std;

struct

edge

;int n,m,s,t,u,v,c,f,maxn,nomore=0;

int sum;

int minflow[maxn]

;int p[maxn]

,dis[maxn]

,inq[maxn]

;edge a[maxn]

[maxn]

;list<

short

>link[maxn]

;int

bfs(

)//找乙個新的增廣路

//每次bfs要把之間存的最小剩餘容量清除

minflow[s]

=maxn;

p[s]=0

;//p陣列存這個節點的流是從哪個節點來的

dis[s]=0

;inq[s]=1

; queue<

short

>q;

q.push

(s);

while

(!q.

empty()

)}}//if(minflow[t]!=0)

} sum=minflow[t];if

(sum==0)

//如果t節點沒有被更新,說明沒有增廣路了

int e=t;

while

(p[e]!=0

)return sum;

}int

main()

int ans=

0,temp,ans2=0;

while

(nomore==0)

cout<

" "<

return0;

}

2.鏈式前向星做法(ac)

//ek,鏈式前向星

#include

#define

maxn

100005

//至少2n大小

using

namespace std;

struct

edge

;int n,m,s,t,u,v,c,f,maxn,nomore=0;

int sum;

int minflow[maxn]

;int p[maxn]

,dis[maxn]

,inq[maxn]

;edge a[maxn]

;int head[maxn]

,cnt_edge=-1

,last[maxn]

;void

add_edge

(int from,

int to,

int c,

int f)

intbfs()

//找乙個新的增廣路

//每次bfs要把之間存的最小剩餘容量清除

minflow[s]

=maxn;

p[s]=0

;//p陣列存這個節點的流是從哪個節點來的

queue<

int>q;

q.push

(s);

dis[s]=0

;inq[s]=1

;while

(!q.

empty()

)}}//if(minflow[t]!=0)

} sum=minflow[t];if

(sum==0)

//如果t節點沒有被更新,說明沒有增廣路了

int e=t;

while

(p[e]!=0

)return sum;

}int

main()

for(

int i=

1;i<=m;i++

)int ans=

0,temp,ans2=0;

while

(nomore==0)

cout<

" "<

return0;

}

網路流 最小費用最大流

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

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

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

網路最大流和最小費用流

from 網路最大流和最小費用流.html 網路流isap演算法的簡單介紹 from 這幾天由於種種原因經常接觸到網路流的題目,這一型別的題給人的感覺,就是要非常使勁的yy才能出來點比較正常的模型。尤其是看了amber最小割應用的文章,裡面的題目思路真是充滿了綿綿不絕的yd思想。然而比賽中,當你yd...