zoukankan      html  css  js  c++  java
  • 最大流bfs

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<math.h>
    using namespace std;
    #define M    9499999
    int c[10004][10004],f[10004][10004];
    int a[1000],pre[1000],n,m,s,t;
    int bfs(int s,int t)
    {
        int flow=0;
        queue<int>q;
        while(1){
            memset(a,0,sizeof(a));
            a[s]=M;
            q.push(s);
            while(!q.empty())
            {
                int u=q.front();q.pop();
                for(int v=1;v<=m;v++)
                if(!a[v]&&f[u][v]<c[u][v])
                {
                    q.push(v);
                    a[v]=min(c[u][v]-f[u][v],a[u]);
                    pre[v]=u;
                } 
                if(a[t])    break;
            }
            if(!a[t])    return flow;
            for(int u=t;u!=s;u=pre[u])
            {
                f[u][pre[u]]-=a[t];
                f[pre[u]][u]+=a[t];
            }
            flow+=a[t];
        }
        return flow;
    }
    int main()
    {
        scanf("%d%d%d%d",&n,&m,&s,&t);
        for(int i=1,u,v,w;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            c[u][v]+=w;
        }
        cout<<bfs(s,t);
        return 0;
    }
  • 相关阅读:
    ASP.NET初识4
    属性
    ASP.NET初识4
    ACCP6.0第九章练习
    ASP.NET初识1
    鼠标指针含义
    ASP.NET初识2
    第三部分
    ASP.NET初识3
    ASP.NET初识5
  • 原文地址:https://www.cnblogs.com/CLGYPYJ/p/6743913.html
Copyright © 2011-2022 走看看