最大流模板

#include <queue>
#include <memory.h>
using namespace std;
const int inf=2147483647;
struct Edge {
    int u,v,c,nx;
}g[M];
int cnt=1,list[N];
int d[N],cur[N];
int s,t;
int ans;

bool Bfs(int v0) {
    queue<int> q;
    while (!q.empty()) q.pop();
    q.push(v0);
    memset(d,0,sizeof d);
    while (!q.empty()) {
        int u=q.front();q.pop();
        for (int i=list[u];i;i=g[i].nx)
        if (g[i].c&&!d[g[i].v]) {
            d[g[i].v]=d[u]+1;
            if (g[i].v==t) return 1;
            q.push(g[i].v);
        }
    }
    return 0;
}

int Maf(int u,int minf) {
    int ret=0,outf;
    if (minf==0||u==t) return minf;
    for (int &i=cur[u];i;i=g[i].nx)
    if (d[g[i].v]==d[u]+1&&g[i].c) {
        outf=Maf(g[i].v,min(minf-ret,g[i].c));
        g[i].c-=outf;
        g[i^1].c+=outf;
        ret+=outf;
        if (ret==minf) return ret;
    }
    return ret;
}

void Dinic() {
    while (Bfs(s)) {
        for (int i=1;i<=t;i++)
        cur[i]=list[i];
        ans+=Maf(s,inf);
    }
}
View Code

 

posted @ 2018-08-05 14:55  Vagari  阅读(96)  评论(0编辑  收藏  举报