P3376 network flow template

emmm

The title has also been very clear

Topic links: https://www.luogu.com.cn/problem/P3376

 

#include <bits/stdc++.h>
using namespace std;
const int maxn=200000+5;
int last[maxn],nxt[maxn*2],to[maxn*2],wi[maxn*2],cnt=-1;
int deep[maxn];
int s,t,n,m;
void add(int u,int v,int w)
{
    to [ ++ cnt] = v;
    wi[cnt]=w;
    nxt[cnt]=last[u];
    last[u]=cnt;
    return ;
}
bool bfs()
{
    queue < int > Q;
    memset(deep,0,sizeof(deep));
    while(!Q.empty())
        Q.pop();
    Q.push(s);
    deep[s]=1;
    do{
        int u=Q.front(); Q.pop();
        for(int i=last[u];i!=-1;i=nxt[i])
            if(wi[i]>0 && deep[to[i]]==0)
            {
                deep[to[i]]=deep[u]+1;
                Q.push(to[i]);
            }
    }while(!Q.empty());
    if(deep[t]==0) return 0;
    return 1;
}
you DFS ( you and, you dist)
{
    if(u==t) return dist;
    for(int i=last[u];i!=-1;i=nxt[i])
        if(deep[to[i]]==deep[u]+1 && wi[i]!=0)
        {
            int di=dfs(to[i],min(dist,wi[i]));
            if(di>0)
            {
                wi [i] - = in;
                wi [i + 1 ] + = in;
                return in;
            }
        }
    return 0;
}
int dinic()
{
    int ans=0;
    while(bfs())
        years + = dfs (s, 100 000 007 );
    return years;
}
int main ()
{
    memset(last,-1,sizeof(last));
    memset(nxt,-1,sizeof(nxt));
    cin>>n>>m>>s>>t;
    for(int i=1;i<=m;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        add(u,v,w);
        add(v,u,0);
    }
    cout<<dinic();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wdxxz3274/p/11965443.html