Analysis of cost flow

Keywords: Graph Theory

Template

EK

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){ if(ch=='-') f=-1;ch=getchar(); }
	while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48);ch=getchar(); }
	return x*f;
}
const int Maxn=5e3+5,Maxm=5e4*2+500,INF=1e8;
struct edge{
	int to,nxt,f,cost;
}e[Maxm];
queue<int> q;
int n,m,S,T,tot=1;
int head[Maxn],incf[Maxn],pre[Maxn],dis[Maxn];
bool vis[Maxn];
inline void add(int u,int v,int c,int d)
{
	e[++tot].to=v; e[tot].nxt=head[u]; head[u]=tot; e[tot].f=c; e[tot].cost=d;
	e[++tot].to=u; e[tot].nxt=head[v]; head[v]=tot; e[tot].f=0; e[tot].cost=-d;
}

inline bool spfa()
{
	memset(dis,0x3f,sizeof dis);
	memset(incf,0,sizeof incf);
	q.push(S);
	incf[S]=INF; dis[S]=0;
	while(!q.empty())
	{
		int now=q.front();q.pop();
		vis[now]=0;
		for(int i=head[now];i>1;i=e[i].nxt)
		{
			int nx=e[i].to;
			if(dis[nx]>dis[now]+e[i].cost&&e[i].f)
			{
				dis[nx]=dis[now]+e[i].cost;
				incf[nx]=min(incf[now],e[i].f);
				pre[nx]=i;
				if(!vis[nx])
				{
					vis[nx]=1;
					q.push(nx);
				}
			}
		}
	}
	return incf[T]>0;
}

inline void EK(int& flow,int& cost)
{
	flow=0; cost=0;
	while(spfa())
	{
		int t=incf[T];
		flow+=t; cost+=t*dis[T];
		for(int i=T;i!=S;i=e[pre[i]^1].to)
		{
			e[pre[i]].f-=t;
			e[pre[i]^1].f+=t;
		}
	}
}

int main()
{
	n=read(); m=read(); S=read(); T=read();
	for(int i=1,a,b,c,d;i<=m;i++)
	{
		a=read(); b=read(); c=read(); d=read();
		add(a,b,c,d);
	}
	int flow,cost;
	EK(flow,cost);
	printf("%d %d",flow,cost);
	return 0;
}

Direct application

  • Drawing creation: left point of S connection ( a i j , 0 ) (a_{ij},0) (aij, 0), right point connection T ( b i j , 0 ) (b_{ij},0) (bij, 0), connection between left point and right point ( I N F , C i j ) (INF,C_{ij}) (INF,Cij​)
  • Method: run the maximum cost and the maximum flow
  • Output scheme: see which side in the middle has a reverse side capacity greater than 0 and meets the point limit

The bipartite graph of cost flow has the best matching

  • Drawing creation: left point of S connection ( a i j , 0 ) (a_{ij},0) (aij, 0), right point connection T ( b i j , 0 ) (b_{ij},0) (bij, 0), connection between left point and right point ( I N F , C i j ) (INF,C_{ij}) (INF,Cij​)
  • Method: run the maximum cost maximum flow, or the minimum cost maximum flow
  • Output scheme: enumerate the middle edges to see the capacity

Maximum weight disjoint path

  • Mapping: dismantle points and add cost and flow restrictions to the edge between the entry point and the exit point
  • Method: run the maximum cost and the maximum flow
  • Output scheme: look at the flow of each side. If the flow of this side is INF, look at its reverse side

Grid graph model

  • Mapping: you can find the rule and use get (function) to assign a unique number to each point. Of course, it can also be used f l a g flag flag array mark to determine whether the point has a number. If so, return this value. If not, assign it to + + cnt
  • Method: if the weight is already on the edge, add the cost to the edge, otherwise, dismantle the point
  • Output scheme: see flow

Disassembly point

  • Mapping: if a point has many restrictions and schemes, you can consider splitting the point, and then transferring different schemes and adding different edges, you can use this to run the maximum flow and find the result.
  • Method: it may not be to limit a point, but to classify and analyze the flow like a hierarchical graph
  • Output scheme: see flow

Upper and lower bounds of cost flow feasible flow

  • Mapping: the upper and lower bounds of the maximum flow are the same. Look A [ i ] A[i] Plus or minus A[i],
    If A [ i ] > 0 A[i]>0 If A[i] > 0, build the edge of A[i] from S to this point, otherwise < 0, build it to T a b s ( A [ i ] ) abs(A[i]) Edge of abs(A[i])
  • Methods: the maximum flow on the new graph is the feasible flow of the original graph. Find a feasible scheme in the feasible flow and calculate the minimum cost maximum flow = > (new graph) minimum cost feasible flow
  • Output scheme: see the flow and cost on the side of the answer requirements

Posted by gukii on Sat, 06 Nov 2021 20:09:01 -0700