网络流 EK算法

int n;
int pre[maxn];
bool vis[maxn];
int mp[maxn][maxn];
const int inf=1e9;
bool bfs(int s,int t)
{
    queue<int> q;
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    pre[s]=s;
    vis[s]=true;
    q.push(s);
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        for(int i=1;i<=n;i++)
        {
            if(mp[now][i]&&!vis[i])
            {
                pre[i]=now;
                vis[i]=true;
                if(i==t) return;
                q.push(i);
            }
        }
    }
    return false;
}
int ek(int s,int t)
{
    int ans=0;
    while(bfs(s,t))
    {
        int minum=inf;
        for(int i=t;i!=s;i=pre[i])
        {
            mi=min(minum,mp[pre[i]][i]);
        }
        for(int i=t;i!=s;i=pre[i])
        {
            mp[pre[i]][i]-=minum;
            mp[i][pre[i]]+=minum;
        }
        ans+=minum;
    }
    return ans;
}

  

猜你喜欢

转载自www.cnblogs.com/zyf3855923/p/9079258.html