板子 网络流算法

Posted pg633

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了板子 网络流算法相关的知识,希望对你有一定的参考价值。

1 .Edmonds-Karp   算法 : 通过反向增光路+bfs 求解最大流

技术分享图片
#include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
         freopen("out","w",stdout);
#define FASTIO  ios::sync_with_stdio(false);
#define CLOCK cout<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";



#define add(u,v,w) e[++tot]=(edge){v,head[u],1},head[u]=tot;
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define DBG2(x,y) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<endl
#define DBG3(x,y,z) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<"\t"<<(#z)<<"="<<z<<endl


#define FOR(i, a, b)  for(int i=(a); i<(b); i++)
#define REP(i, a, b)  for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)
#define Loop(i,u) for(int i =head[u];i;i=e[i].nxt)

#define all(x) x.begin(),x.end()
#define low(x) (x)&(-x)
#define pb push_back
typedef long long ll;
typedef double dl;
const int sz = 222;
const int inf = 0x3ffffff;
int m, n, x, y, z, g[sz][sz], pre[sz], flow[sz], maxflow;
queue<int>q ;

 
#if 1
int bfs(int s, int t)
{
    while (!q.empty()) q.pop();
    REP(i, 1, n) pre[i] = -1;
    pre[s] = 0;
    flow[s] = inf;
    q.push(s);
    while (!q.empty())
    {
        int x = q.front() ; q.pop();
        if (x == t)break;
        REP(i, 1, n)
        {
            if (g[x][i] > 0 && pre[i] == -1)
            {
                pre[i] = x;
                flow[i] = min(flow[x], g[x][i]);
                q.push(i);
            }
        }
    }
    if (pre[t] == -1) return -1;
    else return flow[t];

}

#endif


void EK(int s, int t)
{
    int increase = 0;
    while ( (increase = bfs(s, t)) != -1)
    {
        int k = t;
        while (k != s)
        {
            int last = pre[k];
            g[last][k] -= increase;
            g[k][last] += increase;
            k = last;
        }
        maxflow += increase;
    }
}
int main()
{
    LOACL
    cin >> m >> n;
    REP(i, 1, m)
    {
        cin >> x >> y >> z;
        g[x][y] += z;
    }
    EK(1, n);
    cout << maxflow;
}
View Code

 2Dinic  算法  BFS 分层 DFS 找最大流

技术分享图片
#include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
         freopen("out","w",stdout); 
#define FASTIO  ios::sync_with_stdio(false);
#define CLOCK cout<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";


const int   inf = 987654321;
const int    sz = (int)100010;
const int   mod = (int)1e9 + 7;
const int sqrtn = 300; 

#define add(u,v,w) e[++tot]=(edge){v,head[u],w},head[u]=tot; 
#define CLR(arr,val) memset(arr,val,sizeof(arr)) 
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define DBG2(x,y) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<endl
#define DBG3(x,y,z) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<"\t"<<(#z)<<"="<<z<<endl
 

#define FOR(i, a, b)  for(int i=(a); i<(b); i++)
#define REP(i, a, b)  for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)
#define Loop(i,u) for(int i =head[u];i;i=e[i].nxt)

#define all(x) x.begin(),x.end()
#define low(x) (x)&(-x)
#define pb push_back
typedef long long ll; 
typedef double dl; 
struct edge
{
    int v,nxt,w;
}e[sz<<1];
int head[sz],tot=-1,level[sz];
int n,m,s,t,u,v,w;
int bfs(int s,int t)
{
    CLR(level,0);
    level[s]=1;
    queue<int>q ;
    q.push(s);
    while(!q.empty())
    {
        int v =q.front();q.pop();
        Loop(i,v)
        {
            if(e[i].w> 0 &&!level[e[i].v])
            {
                level[e[i].v]=level[v]+1;
                q.push(e[i].v);
            }
        }
    }
    return level[t];
}
int dfs(int v,int t,int f)
{
    if(v==t) return f;
    int add =0 ;
    Loop(i,v)
    {
        if(e[i].w> 0 && level[v]+1==level[e[i].v])
        {
            int d =dfs(e[i].v,t,min(f-add,e[i].w));
            if(d>0)
            {
                e[i].w-=d;
                e[i^1].w+=d;
                add+=d;
            }
            else level[e[i].v] =-1;
        }

        if(add>=f)break;
    }
    return add;
}
int Dinic(int s,int t)
{
    int flow = 0;
    while(bfs(s,t))
        flow+=dfs(s,t,0x3f3f3f3f);
    return flow;
}

int main()
{
    LOACL
    cin>>n>>m>>s>>t;
    REP(i,1,m) 
    {
        cin>>u>>v>>w;
        add(u,v,w);add(v,w,0);
    }
    cout<<Dinic(s,t);
}
View Code

 

以上是关于板子 网络流算法的主要内容,如果未能解决你的问题,请参考以下文章

网络流板子/费用流板子 2018南京I题+2016青岛G题

COGS 14 [网络流24题] 搭配飞行员 网络流板子题

网络流板子

板子网络流

最大流算法和理论的简单理解

网络流板子