蒟蒻求助网络流EK算法

P3376 【模板】网络最大流

Miss_dijkstra @ 2019-11-08 21:25:29

#include<bits/stdc++.h>
using namespace std;
const int N=201010;
const int INF=0x3f3f3f3f;
int n,m,s,t;
int top=1,head[N];
bool vis[N];
struct Node
{
    int to,val,nxt;
};
Node node[N];
struct PRE
{
    int to,edge;
};
PRE pre[N];
inline int read()
{
    int s=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch<='9'&&ch>='0')
    {
        s=(s<<1)+(s<<3)+ch-'0';
        ch=getchar();
    }
    return s*f;
}
void addedge(int u,int v,int w)
{
    node[++top].to=v;
    node[top].val=w;
    node[top].nxt=head[u];
    head[u]=top;
}
bool bfs()
{
    queue<int> q;
    memset(vis,false,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    vis[s]=true;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();

        q.pop();
        for(int i=head[u];i;i=node[i].nxt)
        {
            int d=node[i].to;

            if(!vis[d]&&node[i].val)
            {
                pre[d].to=u;
                pre[d].edge=i;
                if(d==t) return true;
                vis[d]=true;
                q.push(d);
            }
        }
    }
    return false;
}
int Miss_dijkstra()
{
    int ans=0;
    while(bfs())
    {
        int tmp=INF;
        for(int i=t;i!=s;i=pre[i].to)
        {

            tmp=min(tmp,node[pre[i].edge].val);
        }

        for(int i=t;i!=s;i=pre[i].to)
        {//cout<<i<<endl;
            node[pre[i].edge].val-=tmp;
            node[pre[i].edge^1].val+=tmp;
        }
        ans+=tmp;
        //printf("%d\n",tmp);
        //system("pause");
    }
    return ans;
}
int main()
{
    n=read();m=read();s=read();t=read();
    for(int i=1;i<=m;i++)
    {
        int u,v,w;
        u=read();v=read();w=read();
        addedge(u,v,w);
        addedge(v,u,0);
    }
    /*for(int j=1;j<=n;j++)
    {
        for(int i=head[j];i;i=node[i].nxt)
        {
            printf("%d ",node[i].nxt);
        }
        puts("");
    }*/
   printf("%d",Miss_dijkstra());
}

by Miss_dijkstra @ 2019-11-08 21:26:41

bfs里的

 if(!vis[d]&&node[i].val)
{
。。。
}

改为

 if(vis[d]&&!node[i].val) continue;

。。。

就会死循环


by __gcd @ 2019-11-08 21:31:12

@Miss_dijkstra

if(vis[d]||!node[i].val) continue;

应该是这样吧


by 禰豆子 @ 2019-11-08 21:31:15

你把&&换成||


by Miss_dijkstra @ 2019-11-08 21:31:48

@梁宸铭123 哦脑子抽了


by 辰星凌 @ 2019-11-08 21:38:19

%%%网络流巨佬


by Miss_dijkstra @ 2019-11-08 22:01:53

@辰星凌 Orz Rank165大佬


by 辰星凌 @ 2019-11-08 22:03:06

@Miss_dijkstra stOrz


by jon666 @ 2019-11-12 09:29:08

这题EK能让过的吗???


by Miss_dijkstra @ 2019-11-13 22:04:16

@jon666 能啊


|