#includeusing namespace std;
const int maxv=10010;
const int maxm=10010;
const int inf=1<<30;
typedef pairp;//第乙個值是邊權,第二個值是邊終點
struct edge;
int v,h[maxv],dist[maxv],prevv[maxv],preve[maxv];
vectorg[maxv];
void add_edge(int from,int to,int cap,int cost));
g[to].push_back((edge));
}int min_cost_flow(int s,int t,int f)}}
if(dist[t]==inf)return -1; //目標點未被更新,無法到達
for(int v=0;v>n>>m)
cout<}
return 0;
}最小費用流poj2135
題意:給定一無向圖,要從1點到n點再返回1點,每條邊最多走一次,問最短需要走多遠。
分析:最小費用最大流,把題意看成是要找兩條無交集的從1到n的路線,使距離和最小。圖中的點和邊就是網路流圖中的點和邊。為保證無交集,我們把每條邊的流量設定為1,而源發出的流量和匯接收的流量均為2。每條邊的費用就是該邊在原圖中的權值。
注意:有重邊,所以要用鄰接表。因為是無向圖,所以要在加邊時,原圖中的一條邊要變成網路流圖中的兩條邊(如果把反向負權費用邊也算上就總共4條邊)。
sample input
4 51 2 1
2 3 1
3 4 1
1 3 2
2 4 2
sample output
6
最小費用流
include include define maxn 61 define maxv maxn maxn 2 1 define maxe maxv 5 define oo 2147483647 define min a,b a b b a define maxq maxe using namespa...
最小費用流
int v 頂點數 vector g max v int dist max v 最短距離 int prev max v 最短路中前驅結點對應的點 int pree max v 最短路中前驅結點對應的邊 void addedge int from,int to,int cap,int cost 求從s...
最小費用流
acm模版 最小費用流 o v e f init network g g.build v,e call g.mincost s,t flow g.flow cost g.cost 注意 spfa增廣,實際複雜度遠遠小於o v e define typef int type of flow defin...