POJ 2396 Budget 上下界网络流
这是一道比较基础的上下界网络流了上下界网络流的算法完全是参考论文《一种简易的方法求解流量有上下界的网络 》 还有 《最大流在信息学竞赛中应用的一个模型 》
然后 说的也是比较详细
具体的解法不再多说了,网络流的算法一直是比较麻烦,代码量还大,我看了好久才明白算法是啥意思。
刚开始比较迷惑为啥加了一对源汇了,又加一对。 后来明白了,对本题而言,刚开始是一个有源汇的网络,
就是我们加的第一对的源汇,而我们要转化为一个无源汇的网络,就在汇到源加一条无穷容量的边,这样就满足了定义的要求,后来再加一对的源汇,才是论文中说的附加源汇
代码比较肥硕
输出答案的时候。刚开始觉得挺蛋疼,后来发现边都是按顺序加的,瞬间觉得世界又美好了
我的模板里,边里存的cap表示这条边还有多少流量可用,flow就是现在已经使用的流量
[cpp]
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define MAXN 555
#define MAXM 555555
#define INF 1000000007
using namespace std;
struct node
{
int ver; // vertex
int cap; // capacity
int flow; // current flow in this arc
int next, rev;
}edge[MAXM];
int dist[MAXN], numbs[MAXN], src, des, n;
int head[MAXN], e;
void add(int x, int y, int c)
{ //e记录边的总数
edge[e].ver = y;
edge[e].cap = c;
edge[e].flow = 0;
edge[e].rev = e + 1; //反向边在edge中的下标位置
edge[e].next = head[x]; //记录以x为起点的上一条边在edge中的下标位置
head[x] = e++; //以x为起点的边的位置
//反向边
edge[e].ver = x;
edge[e].cap = 0; //反向边的初始可行流量为0
edge[e].flow = 0;
edge[e].rev = e - 1;
edge[e].next = head[y];
head[y] = e++;
}
void rev_BFS()
{
int Q[MAXN], qhead = 0, qtail = 0;
for(int i = 1; i <= n; ++i)
{
dist[i] = MAXN;
numbs[i] = 0;
}
Q[qtail++] = des;
dist[des] = 0;
numbs[0] = 1;
while(qhead != qtail)
{
int v = Q[qhead++];
for(int i = head[v]; i != -1; i = edge[i].next)
{
if(edge[edge[i].rev].cap == 0 || dist[edge[i].ver] < MAXN)continue;
dist[edge[i].ver] = dist[v] + 1;
++numbs[dist[edge[i].ver]];
Q[qtail++] = edge[i].ver;
}
}
}
void init()
{
e = 0;
memset(head, -1, sizeof(head));
}
int maxflow()
{
int u, totalflow = 0;
int Curhead[MAXN], revpath[MAXN];
for(int i = 1; i <= n; ++i)Curhead[i] = head[i];
u = src;
while(dist[src] < n)
{
if(u == des) // find an augmenting path
{
int augflow = INF;
for(int i = src; i != des; i = edge[Curhead[i]].ver)
augflow = min(augflow, edge[Curhead[i]].cap);
for(int i = src; i != des; i = edge[Curhead[i]].ver)
{
edge[Curhead[i]].cap -= augflow;
edge[edge[Curhead[i]].rev].cap += augflow;
edge[Curhead[i]].flow += augflow;
edge[edge[Curhead[i]].rev].flow -= augflow;
}
totalflow += augflow;
u = src;
}
int i;
for(i = Curhead[u]; i != -1; i = edge[i].next)
if(edge[i].cap > 0 && dist[u] == dist[edge[i].ver] + 1)break;
if(i != -1) // find an admissible arc, then Advance
{
Curhead[u] = i;
revpath[edge[i].ver] = edge[i].rev;
u = edge[i].ver;
}
else // no admissible arc, then relabel this vertex
{
if(0 == (--numbs[dist[u]]))break; // GAP cut, Important!
Curhead[u] = head[u];
int mindist = n;
for(int j = head[u]; j != -1; j = edge[j].next)
if(edge[j].cap > 0)mindist = min(mindist, dist[edge[j].ver]);
&nbs
补充:软件开发 , C++ ,