zoukankan      html  css  js  c++  java
  • 【网络流】【Dinic】【Next Array】Dinic模板

    注意:有时加边不一定要加反向弧。

    Next Array版。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<queue>
     5 using namespace std;
     6 #define INF 2147483647
     7 #define MAXN 20011
     8 #define MAXM 600301
     9 int v[MAXM],cap[MAXM],en,first[MAXN],next[MAXM];
    10 int d[MAXN],cur[MAXN];
    11 queue<int>q;
    12 int n,m,S,T;
    13 void Init_Dinic(){memset(first,-1,sizeof(first)); en=0;}
    14 void AddEdge(const int &U,const int &V,const int &W)
    15 {
    16     v[en]=V; cap[en]=W;
    17     next[en]=first[U]; first[U]=en++;
    18     v[en]=U; cap[en]=0;
    19     next[en]=first[V]; first[V]=en++;
    20 }
    21 bool bfs()
    22 {
    23     memset(d,-1,sizeof(d)); q.push(S); d[S]=0;
    24     while(!q.empty())
    25       {
    26           int U=q.front(); q.pop();
    27           for(int i=first[U];i!=-1;i=next[i])
    28             if(d[v[i]]==-1 && cap[i])
    29               {
    30                 d[v[i]]=d[U]+1;
    31                 q.push(v[i]);
    32               }
    33       }
    34     return d[T]!=-1;
    35 }
    36 int dfs(int U,int a)
    37 {
    38     if(U==T || !a) return a;
    39     int Flow=0,f;
    40     for(int &i=cur[U];i!=-1;i=next[i])
    41       if(d[U]+1==d[v[i]] && (f=dfs(v[i],min(a,cap[i]))))
    42         {
    43           cap[i]-=f; cap[i^1]+=f;
    44           Flow+=f; a-=f; if(!a) break;
    45         }
    46     if(!Flow) d[U]=-1;
    47     return Flow;
    48 }
    49 int max_flow()
    50 {
    51     int Flow=0,tmp=0;
    52     while(bfs())
    53       {
    54           memcpy(cur,first,(n+5)*sizeof(int));
    55           while(tmp=dfs(S,INF)) Flow+=tmp;
    56       }
    57     return Flow;
    58 }
  • 相关阅读:
    Python startswith()函数 与 endswith函数
    Oracle spool 小结
    表空间(TableSpace)
    Python logger模块
    Mysql rpm安装
    Python json与pickle
    Python 生成器总结
    Python 装饰器的总结
    eclipse 乱码问题总结
    Eclipse 中出现红色下划波浪线与红色感叹号
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/4148722.html
Copyright © 2011-2022 走看看