zoukankan      html  css  js  c++  java
  • 网络流模板

    Dinic

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    const int N=1111,M=1111,INF=0x3f3f3f3f;
    struct node
    {
        int v,c,next;
    }e[M];
    int head[N],d[N],q[N];
    int n,m,l,r,cnt;
    void add(int u,int v,int c)
    {
        e[cnt].v=v,e[cnt].c=c;
        e[cnt].next=head[u],head[u]=cnt++;
        e[cnt].v=u,e[cnt].c=0;
        e[cnt].next=head[v],head[v]=cnt++;
    }
    void init()
    {
        memset(head,-1,sizeof(head));
        cnt=0;
    }
    int bfs(int s,int t)
    {
        l=r=0;
        memset(d,-1,sizeof(d));
        q[++r]=s;d[s]=0;int u,v,i;
        while(l<r)
        {
            u=q[++l];
            for(i=head[u];i!=-1;i=e[i].next)
            {
                v=e[i].v;
                if(e[i].c&&d[v]==-1)
                {
                    d[v]=d[u]+1;
                    q[++r]=v;
                }
            }
        }
        if(d[t]==-1)    return 0;
        return 1;
    }
    int dfs(int u,int flow)
    {
        if(u==t)    return flow;
        int v,a,i,ans=0;
        for(i=head[u];i!=-1;i=e[i].next)
        {
            v=e[i].v;
            if(d[v]==d[u]+1&&e[i].c&&(a=dfs(v,min(flow,e[i].c))))
            {
                e[i].c-=a;
                e[i^1].c+=a;
                ans+=a;
                flow-=a;
                if(!flow)    break;
            }
        }
        if(ans)    return ans;
        d[u]=-1;
        return 0;
    }
    int dinic(int s,int t)
    {
        int ans=0;
        while(bfs(s,t))
            ans+=dfs(s,INF);
        return ans;
    }
    Dinic

    EK

    void updata(int i,int flow)
    {
        while(pre[i]!=-1)
        {
            c[i][pre[i]]+=flow;
            c[pre[i]][i]-=flow;
            i=pre[i];
        }
    }
    int bfs()
    {
        memset(p,0,sizeof(p));
        memset(pre,-1,sizeof(pre));
        queue<int> q;
        q.push(1);
        p[1]=1;
        int min_f=1<<28,i,cur;
        while(!q.empty())
        {
            cur=q.front();
            q.pop();
            if(cur==n)
                break;
            for(i=1;i<=n;i++)
            {
                if(c[cur][i]&&!p[i])
                {
                    p[i]=1;
                    pre[i]=cur;
                    min_f=min(min_f,c[cur][i]);
                    q.push(i);
                }
            }
        }
        if(pre[n]==-1)
            return 0;
        return min_f;
    }
    int EK()
    {
        int tmp,ans=0;
        do
        {
            tmp=bfs();
            updata(n,tmp);
            ans+=tmp;
        }
        while(tmp!=0);
        return ans;
    }
    EK

    ISAP

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    #define INF 0x3f3f3f3f
    
    const int N=1100;
    const int M=100000;
    int h[N],gap[N],head[N];
    int cnt,n,m,s,t; 
    struct node
    {
        int v,c,next;
    }e[M];
    
    void init()
    {
        memset(head,-1,sizeof(head));
        cnt=0;
    }
    void add(int u,int v,int w)
    {
        e[cnt].v=v,e[cnt].c=w;
        e[cnt].next=head[u];head[u]=cnt++;
        e[cnt].v=u,e[cnt].c=0;
        e[cnt].next=head[v];head[v]=cnt++;
    }
    int dfs(int u,int flow)
    {
        if(u==t)    return flow;
        int c=flow,a,i,v,minh=t;
        for(i=head[u];i!=-1;i=e[i].next)
        {
            if(e[i].c)
            {
                v=e[i].v;
                if(h[v]==h[u]-1)
                {
                    a=min(c,e[i].c);
                    a=dfs(v,a);
                    e[i].c-=a;
                    e[i^1].c+=a;
                    c-=a;
                    if(h[s]>t)  return flow-c;
                    if(!c)  break;
                }
                minh=min(minh,h[v]);
            }
        }
        if(c==flow)
        {
            if(--gap[h[u]]==0)    h[s]=t+1;
            h[u]=minh+1;
            ++gap[h[u]];
        }
        return flow-c;
    }
    int isap() 
    {
        memset(gap,0,sizeof(gap));
        memset(h,0,sizeof(h));
        int ans=0;gap[0]=t+1;
        while(h[s]<=t)
            ans+=dfs(s,INF);
        return ans;
    }
    ISAP

     笔者建议,ISAP不是那么好理解,如果不能完全理解,又不能使用模板的情况下,请背住DINIC。

  • 相关阅读:
    64.Find the Duplicate Number(发现重复数字)
    63.Perfect Squares(完美平方数)
    62.Longest Valid Parentheses(最长的有效括号)
    61.Merge k Sorted Lists(合并k个排序链表)
    60.Median of Two Sorted Arrays(两个排序数组的中位数)
    59.Target Sum(目标和)
    58.Partition Equal Subset Sum(判断一个数组是否可以分成和相等的两个数组)
    57.Queue Reconstruction by Height(按身高重建对列)
    56.Decode String(解码字符串)
    55.Top K Frequent Elements(出现次数最多的k个元素)
  • 原文地址:https://www.cnblogs.com/L-King/p/5313038.html
Copyright © 2011-2022 走看看