Front
• a flow network (Network Flow) \ (G = (V, E) \) is a directed graph, each edge \ ((u, v) \ in E \) has a non-negative capacity (Capacity) \ ( C (U, V)> = 0 \) . for not \ (E \) of \ ((U, V) \) , a predetermined \ (C (U, V) = 0 \)
• has two special nodes point: source (source) \ (S (S \ in V) \) and sinks (sink) \ (T (T \ in V) \) .
• assumed that for any other point v, the presence path \ (s \ rarr v \ rarr t \)
• a side stream is a function of the \ (F (U, V) \) , the flow rate function \ (f (x, y) \, (x \ in V, y \ in V) \) is satisfied:
• capacity limitation: \ (\ FORALL (U, V), \; 0 \ F Le (U, V) \ C Le (U, V) \)
• Symmetry: \ (f (U, v) = - f (v, U) \)
• balance: For not \ (S \) or \ (T \) other points \ (U \) , there are \ (\ sum \ limits_ {v \ in V} f (u, v) = 0 \)
• the flow rate defines the entire network is (i.e. from s effluent flow rate): \ (| F | = \ SUM \ limits_ {V \ in V} F (s, V) \)
• Maximum flow problem: find the stream function \ (F \) , so that the maximum flow of the network
•other
Equivalent conditions:
. 1) is the maximum flow F
2) no residual network augmenting path
3) the existence of a cut (S, T), | f | = c (S, T)
Every time looking for the least number of sides augmenting path be augmented
• Theorem: wide by up to O (nm) time
• Edmonds-Karp: BFS find each augmenting path, additional maintenance information
• Dinic: BFS multiple hierarchical graph structure , DFS find augmenting paths. Additional information:
levels (from the starting point to the value of u)
The maximum flow
luogu3376 network maximum flow [template]
Every bfs find augmenting paths until no augmenting path graph
#include<bits/stdc++.h>
using namespace std;
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)>(y)?(y):(x))
const int N=10000+5,M=100000+5,inf=0x3f3f3f3f,P=19650827;
int n,m,s,t,maxflow=0,pre[N],incf[N];
template <class t>void rd(t &x){
x=0;int w=0;char ch=0;
while(!isdigit(ch)) w|=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
x=w?-x:x;
}
int head[N],tot=1;
struct edge{int v,w,nxt;}e[M<<1];
void add(int u,int v,int w){
e[++tot]=(edge){v,w,head[u]},head[u]=tot;
}
bool vis[N];
queue<int> q;
bool bfs(){
while(!q.empty()) q.pop();
memset(vis,0,sizeof(vis));
q.push(s),vis[s]=1,incf[s]=inf;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u],v,w;i;i=e[i].nxt)
if(w=e[i].w){
v=e[i].v;
if(vis[v]) continue;
incf[v]=Min(incf[u],w),pre[v]=i;
q.push(v),vis[v]=1;
if(v==t) return 1;
}
}
return 0;
}
void upd(){
int x=t;
while(x!=s){
int i=pre[x];
e[i].w-=incf[t],e[i^1].w+=incf[t];
x=e[i^1].v;
}
maxflow+=incf[t];
}
int main(){
rd(n),rd(m),rd(s),rd(t);
for(int i=1,u,v,w;i<=m;++i)
rd(u),rd(v),rd(w),add(u,v,w),add(v,u,0);
while(bfs())
upd();
printf("%d",maxflow);
return 0;
}