网络流·最大流模板

链接

https://www.luogu.org/problemnew/show/P3376

大意

给定一张有向无环图,求它的最大流

思路

套模板

代码(不能过样例但是AC了)

原因是因为l没有变成-1,而却有0的点,但是由于样例过水没有找出

// luogu-judger-enable-o2
#include<cstring>
#include<cstdio>
#include<queue>
#define min(a,b) a<b?a:b
#define N 10001
#define M 100001
using namespace std;int f,p,k,n,m;char c;
int read()
{
    f=0;p=1;
    while(c=getchar(),c<=47||c>=58) if(c=='-') p=-1;f=(f<<3)+(f<<1)+c-48;
    while(c=getchar(),c>=48&&c<=57) f=(f<<3)+(f<<1)+c-48;
    return p*f;
}
struct node{int next,to,w;}e[M<<1];int l[N],tot,d[N],s,t;
void add(int u,int v,int w)
{
    e[tot].next=l[u];e[tot].to=v;e[tot].w=w;l[u]=tot++;
    e[tot].next=l[v];e[tot].to=u;e[tot].w=0;l[v]=tot++;
    return;
}
bool bfs()
{
    memset(d,-1,sizeof(d));
    queue<int>q;d[s]=0;q.push(s);
    while(q.size())
    {
        int x=q.front();q.pop();
        for(int i=l[x];i;i=e[i].next)
        {
            int y=e[i].to;
            if(e[i].w&&d[y]==-1)
            {
                d[y]=d[x]+1;
                q.push(y);
                if(y==t) return true;
            }
        }
    }
    return false;
}
int dfs(int x,int flow)
{
    if(x==t||!flow) return flow;
    int rest=0,k=0;
    for(int i=l[x];i;i=e[i].next)
    {
        int y=e[i].to;
        if(d[x]+1==d[y]&&e[i].w)
        {
            f=dfs(y,min(flow-rest,e[i].w));
            e[i].w-=f;rest+=f;e[i^1].w+=f;
        }
    }
    if(!rest) d[x]=0;
    return rest;
}
int dinic()
{
    int r=0;
    while(bfs()) r+=dfs(s,2147483647);
    return r;
}
int main()
{
    n=read();m=read();s=read();t=read();
    for(int i=1,x,y,z;i<=m;i++) x=read(),y=read(),z=read(),add(x,y,z);
    printf("%d",dinic());
}

代码(能过样例能AC)

但是很慢

// luogu-judger-enable-o2
#include<cstring>
#include<cstdio>
#include<queue>
#define min(a,b) a<b?a:b
#define N 10001
#define M 100001
using namespace std;int f,p,k,n,m,x,y,z;char c;
int read()
{
    f=0;p=1;
    while(c=getchar(),c<=47||c>=58) if(c=='-') p=-1;f=(f<<3)+(f<<1)+c-48;
    while(c=getchar(),c>=48&&c<=57) f=(f<<3)+(f<<1)+c-48;
    return p*f;
}
struct node{int next,to,w;}e[M<<1];int l[N],tot,d[N],s,t;
void add(int u,int v,int w)
{
    e[tot].next=l[u];e[tot].to=v;e[tot].w=w;l[u]=tot++;
    e[tot].next=l[v];e[tot].to=u;e[tot].w=0;l[v]=tot++;
    return;
}
bool bfs()
{
    memset(d,-1,sizeof(d));
    queue<int>q;d[s]=0;q.push(s);
    while(q.size())
    {
        int x=q.front();q.pop();
        for(int i=l[x];i!=-1;i=e[i].next)
        {
            int y=e[i].to;
            if(e[i].w&&d[y]==-1)
            {
                d[y]=d[x]+1;
                q.push(y);
                if(y==t) return true;
            }
        }
    }
    return false;
}
int dfs(int x,int flow)
{
    if(x==t) return flow;
    int rest=0,k;
    for(int i=l[x];i!=-1;i=e[i].next)
    {
        int y=e[i].to;
        if(d[x]+1==d[y]&&e[i].w)
        {
            f=dfs(y,min(flow-rest,e[i].w));
            if(!f) d[y]=0;
            e[i].w-=f;rest+=f;e[i^1].w+=f;
        }
    }
    if(!rest) d[x]=0;
    return rest;
}
int dinic()
{
    int r=0;
    while(bfs()) r+=dfs(s,2147483647);
    return r;
}
int main()
{
    memset(l,-1,sizeof(l));//多了这一行
    n=read();m=read();s=read();t=read();
    for(int i=1;i<=m;i++) x=read(),y=read(),z=read(),add(x,y,z);
    printf("%d",dinic());
}

猜你喜欢

转载自blog.csdn.net/xuxiayang/article/details/80755053