最小(大)費用流

2021-06-26 23:56:37 字數 1656 閱讀 9028

演算法很簡單 spfa+增廣路

1、首先按照邊的費用spfa最短路(如果是求最大費用可用最長路)

2、接著增光路的可行流的調節

3、重複1 2 直到最短路無法到達匯點為止

挺簡單的注意細節即可

poj 3422

這題的建圖很精妙 贊

/** this code is made by linmeichen

* problem:

* type of problem:

* thinking:

* feeling:

*/#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

typedef long long lld;

typedef unsigned int ud;

#define inf_max 0x3f3f3f3f

#define eatline() char chch;while((chch=getchar())!='\n')continue;

#define memsetmax(a) memset(a,0x3f,sizeof a)

#define memsetzero(a) memset(a,0,sizeof a)

#define memsetmin(a) memset(a,-1,sizeof a)

#define memsetfalse(a) memsetzero(a)

#define pq priority_queue

#define q queue

#define maxn 5010

#define maxm 100008

struct edge

e[maxm];

int cnt;

int head[maxn];

int map[55][55];

int dis[maxn], mark[maxn], pre[maxn];

bool spfa(int s, int t)}}

}return dis[t] != inf_max;

}int argument(int s, int t)

for (int i = pre[t]; i!= -1; i = pre[e[i ^ 1].v])

return maxflow;

}int maxcostflow(int s, int t)

void add_edge(int u, int v, int f, int c)

int main()

for (int i = 1; i <= n;i++)

for (int j = 1; j < n; j++)

for (int i = 1; i < n;i++)

for (int j = 1; j <= n; j++)

add_edge(s, 0, k, 0);

add_edge(2*n*n-1, t, k, 0);

printf("%d\n", -maxcostflow(s, t));

}return 0;

}

最小費用流

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...