网络流模板大全

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#include 
#include 
#include 
#include 
#include 
#include 
const int MAXN = 2999;//点数的最大值
const int MAXM = 2222;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{
	int to,next,cap,flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];
void init()
{
	tol = 0;
	memset(head,-1,sizeof (head));
}
//加边,单向图三个参数,双向图四个参数
void addedge (int u,int v,int w,int rw=0)
{ 
	edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];
	edge[tol].flow = 0;head[u] = tol++;
	edge[tol].to = u;edge[tol].cap = rw;edge[tol]. next = head[v];
	edge[tol].flow = 0;head[v]=tol++;
}
//输入参数:起点、终点、点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start,int end, int N)
{
	memset(gap,0,sizeof(gap));
	memset(dep,0,sizeof(dep));
	memcpy(cur,head,sizeof(head));
	int u = start;
	pre[u] = -1;
	gap[0] = N;
	int ans = 0;
	int i;
	while(dep[start] < N)
	{
		if(u == end)
		{
			int Min = INF;
			for( i = pre[u];i != -1; i = pre[edge[i^1]. to])
			{
				if(Min > edge[i].cap - edge[i]. flow)
					Min = edge[i].cap - edge[i].flow;
			}
			for( i = pre[u];i != -1; i = pre[edge[i^1]. to])
			{
				edge[i].flow += Min;
				edge[i^1].flow -= Min;
			}
			u = start;
			ans += Min;
			continue;
		}
		bool flag =  false;
		int v;
		for( i = cur[u]; i != -1;i = edge[i].next)
		{
			v = edge[i]. to;
			if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
			{
				flag =  true;
				cur[u] = pre[v] = i;
				break;
			}
		}
		if(flag)
		{
			u = v;
			continue;
		}
		int Min = N;
		for( i = head[u]; i !=  -1;i = edge[i]. next)
		{
			if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
			{
				Min = dep[edge[i].to];
				cur[u] = i;
			}
		}
		gap[dep[u]]--; 
		if(!gap[dep[u]]) return ans;
		dep[u] = Min+1;
		gap[dep[u]]++;
		if(u != start) u = edge[pre[u]^1].to;
	}
	return ans;
}
int main()
{
	int n,m,a,b,c;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		init();
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			addedge(a,b,c,0);
		}
		printf("%d\n",sap(1,m,m));
	}
	return 0;
}
BFS优化
//13.4 ISAP+bfs 初始化+栈优化
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#include 
#include 
#include 
#include 
#include 
#include 
const int MAXN = 22222;//点数的最大值
const int MAXM = 22222;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{
	int to,next,cap,flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
void init()
{
	tol = 0;
	memset(head,-1,sizeof (head));
}
void addedge (int u,int v,int w,int rw = 0)
{
	edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0;
	edge[tol].next = head[u]; head[u] = tol++;
	edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0;
	edge[tol].next = head[v]; head[v] = tol++;
}
int Q[MAXN];
void BFS(int start,int end)
{
	memset(dep,-1,sizeof(dep));
	memset(gap,0,sizeof(gap));
	gap[0] = 1;
	int front = 0, rear = 0;
	dep[end] = 0;
	Q[rear++] = end;
	while(front != rear)
	{
		int u = Q[front++];
		for(int i = head[u]; i !=  -1; i = edge[i].next)
		{
			int v = edge[i]. to;
			if(dep[v] != -1)continue;
			Q[rear++] = v;
			dep[v] = dep[u] + 1;
			gap[dep[v]]++;
		}
	}
}
int S[MAXN];
int sap(int start,int end, int N)
{
	BFS(start,end);
	memcpy(cur,head,sizeof(head));  
	int top = 0;
	int u = start;
	int ans = 0;
	int i;
	while(dep[start] < N)
	{
		if(u == end)
		{
			int Min = INF;
			int inser;
			for( i = 0;i < top;i++)
			{
				if(Min > edge[S[i]].cap - edge[S[i]].flow)
				{
					Min = edge[S[i]].cap - edge[S[i]].flow;
					inser = i;
				}
			}
			for( i = 0;i < top;i++)
			{
				edge[S[i]]. flow += Min;
				edge[S[i]^1].flow -= Min;
			}
			ans += Min;
			top = inser;
			u = edge[S[top]^1].to;
			continue;
		}
		bool flag =  false;
		int v;
		for( i = cur[u]; i != -1; i = edge[i]. next)
		{
			v = edge[i]. to;
			if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
			{
				flag =  true;
				cur[u] = i;
				break;
			}
		}
		if(flag)
		{
			S[top++] = cur[u];
			u = v;
			continue;
		}
		int Min = N;
		for( i = head[u]; i !=  -1; i = edge[i].next)
		{
			if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
			{
				Min = dep[edge[i].to];
				cur[u] = i;
			}
		}
		gap[dep[u]]--;
		if(!gap[dep[u]]) return ans;
		dep[u] = Min + 1;
		gap[dep[u]]++;
		if(u != start)u = edge[S[--top]^1].to;
	}
	return ans;
} 
int main()
{
	int n,m,a,b,c;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		init();
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			addedge(a,b,c,0);
		}
		printf("%d\n",sap(1,m,m));
	}
	return 0;
}

最小费用流(SPFA)

/*
最小费用最大流,求最大费用只需要取相反数,结果取相反数即可。
点的总数为 N,点的编号 0~N-1
*/
const int MAXN = 10000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
struct Edge
{
    int to,next,cap,flow,cost;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
void init(int n)
{
    N = n;
    tol = 0;
    memset(head,-1,sizeof (head));
}
void addedge (int u,int v,int cap,int cost)
{
    edge[tol].to = v;
    edge[tol].cap = cap;
    edge[tol].cost = cost;
    edge[tol].flow = 0;
    edge[tol].next = head[u];
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = 0;
    edge[tol].cost = -cost;
    edge[tol].flow = 0;
    edge[tol].next = head[v];
    head[v] = tol++;
}
bool spfa(int s,int t)
{
    queueq;
    for(int i = 0; i < N; i++)
    {
        dis[i] = INF;
        vis[i] = false;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = true;
    q.push(s);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = head[u]; i !=  -1; i = edge[i]. next)
        {
            int v = edge[i]. to;
            if(edge[i].cap > edge[i].flow &&
                    dis[v] > dis[u] + edge[i]. cost )
            {
                dis[v] = dis[u] + edge[i]. cost;
                pre[v] = i;
                if(!vis[v])
                {
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t] == -1)return false;
    else return true;
}
//返回的是最大流,cost存的是最小费用
int minCostMaxflow(int s,int t,int &cost)
{
    int flow = 0;
    cost = 0;
    while(spfa(s,t))
    {
        int Min = INF;
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            if(Min > edge[i].cap - edge[i]. flow)
                Min = edge[i].cap - edge[i].flow;
        }
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            edge[i].flow += Min;
            edge[i^1].flow -= Min;
            cost += edge[i]. cost * Min;
        }
        flow += Min;
    }
    return flow;
}


你可能感兴趣的:(模板,网络流)