#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std ;
const int maxn= ;
const int oo=10e9;
typedef struct EDGE{
int u,v;
int c,f,next;
EDGE (){
u=v=c=f=0;
next=-1;
}
EDGE (int a,int b,int d,int e,int g){
u=a;v=b;c=d;f=e;next=g;
}
}E;
E edge[maxn];
int len=0,head[maxn],n;
int vis[maxn],dis[maxn];
int cur[maxn];
int allp;//总的点个数
int S,T;//源点和汇点的编号
void E_add (int s,int t,int x,int y){
edge[++len]=EDGE (s,t,x,y,head[s]);
head[s]=len;
}
int bfs (){
memset (vis,-1,sizeof(vis));
queue <int> q;
int now,i;
q.push (S);
vis[S]=1;
dis[S]=0;
while (!q.empty ()){
now=q.front ();
q.pop ();
for (i=head[now];i!=-1;i=e[i].next){
if (vis[edge[i].v]==-1&&edge[i].c>edge[i].f) {
q.push (edge[i].v);
dis[edge[i].v]=dis[now]+1;
vis[edge[i].v]=1;
}
}
}
return vis[T];
}
void dfs (int x,int fmax){
if (x==-1||flow==0) return 0;
int f,flow=0,&now=cur[x];
for (;now!=-1;now=e[now].next){
if (dis[e[now].v]=dis[now]&&(f=dfs (e[i].v,min(fmax,e[i].c-e[i].f)))){
flow+=f;
e[i].f+=f;
e[i^1].f-=f;
fmax-=f;
if (fnow==0) break;
}
}
return flow;
}
int dinic (){
int flow=0,i;
while (bfs ()){
for (i=1;i<=allp;i++) cur[i]=head[i];
flow+=dfs (S,oo);
}
return flow;
}
自学网络流
最新推荐文章于 2024-06-25 10:59:37 发布