[模板]网络流

网络流中的最大流(isap算法)

const int INF=99999999;
struct node
{
    int v,w,f;
    node *next,*back;
}edge[MAXN*4+10];
node *adj[MAXN+10];
node *ecnt=edge;
int S,T;
int d[MAXN*2+10],vis[MAXN*2+10];
inline void addedge(int u,int v,int w)
{
    node *p=++ecnt;
    p->v=v;
    p->w=w;
    p->f=0;
    p->next=adj[u];
    p->back=p+1;
    adj[u]=p;
     
    p=++ecnt;
    p->v=u;
    p->w=0;
    p->f=0;
    p->next=adj[v];
    p->back=p-1;
    adj[v]=p;
}
int aug(int i,int augco)
{
    if(i==T) return augco;
    int mind=T-1,u,augc=0;
    for(node *p=adj[i];p;p=p->next)
    {
        int v=p->v;
        int w=p->w;
        int f=p->f;
        if(w<=f) continue;
        if(d[i]==d[v]+1)
        {
            u=min(augco-augc,w-f);
            u=aug(v,u);
            p->f+=u;
            p->back->f-=u;
            augc+=u;
            if(d[S]>=T) return augc;
            if(augc==augco) break;
        }
        mind=min(mind,d[v]);
    }
    if(augc==0)
    {
        vis[d[i]]--;
        if(!vis[d[i]])
            d[S]=T;
        d[i]=mind+1;
        vis[d[i]]++;
    }
    return augc;
}
int isap()
{
    int ans=0;
    while(d[S]

网络流中的费用流(lrj的模板,用双端队列优化)

#include
#include
#include
#include
#include
#include
#define MAXN 200
using namespace std;
const int INF=99999999;
struct Edge
{
    int u,v,c,f,cost;
};
struct MCMF
{
    int n,m,s,t;
    vectoredges;
    vectorG[MAXN+10];
    int inq[MAXN+10],d[MAXN+10],p[MAXN+10],a[MAXN+10];
    void init(int n)
    {
        this->n=n;
        for(int i=0;iq;
        q.push_front(s);
        while(!q.empty())
        {
            int u=q.front();
            q.pop_front();
            inq[u]=0;
            for(i=0;if&&d[v]>d[u]+cost)
                {
                    d[v]=d[u]+cost;
                    p[v]=G[u][i];
                    a[v]=min(a[u],c-f);
                    if(!inq[v])
                    {
                        inq[v]=true;
                        q.push_back(v);
                    }
                }
            }
        }
        if(d[t]==INF) return false;
        Flow+=a[t];
        Cost+=d[t]*a[t];
        int u=t;
        while(u!=s)
        {
            edges[p[u]].f+=a[t];
            edges[p[u]^1].f-=a[t];
            u=edges[p[u]].u;
        }
        return true;
    }
    int mc(int s,int t)
    {
        int flow=0,cost=0;
        while(bf(s,t,flow,cost));
        return cost;
    }
};


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