zoukankan      html  css  js  c++  java
  • 最大流增广路算法

    最大流增广路算法

    Edmonds_Karp算法:通过bfs从零流开始不断寻找增广路,当无法在增广时,此时流为最大流

    /*  Edmonds_Karp算法 */
    
    int cap[maxn][maxn],flow[maxn][maxn]; //cap容量,flow流量,相减得残量
    
    int Edmonds_Karp(int s,int t)
    {
        int f=0;
        int p[maxn],a[maxn]; //p记录路径
        queue<int> q;
        memset(flow,0,sizeof(flow)); //初始化流量为0
        while(1){
            memset(a,0,sizeof(a)); //将a置0
            a[s]=INF;  //a[s]处置为INF
            q.push(s);    //s入队
            while(!q.empty()){
                int u=q.front();q.pop();
                for(int v=0;v<=n+2;v++){
                    if(!a[v]&&cap[u][v]-flow[u][v]>0){ //对每条队头u的残量>0的出边,v入队,记录路径;
                        q.push(v);
                        p[v]=u;
                        a[v]=min(a[u],cap[u][v]-flow[u][v]); //这句增广别忘了
                    }
                }
            }
            if(a[t]==0) return f;  //不能再增广时返回f
            for(int u=t;u!=s;u=p[u]){ //沿路径回溯
                flow[p[u]][u]+=a[t];
                flow[u][p[u]]-=a[t];
            }
            f+=a[t]; //增广
        }
    }
    Edmonds_Karp
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    Test
    占位2
    开坑纪念
    function 类型(函数定义)----读书总结
    css位元素 after
    算法-哈希表
    CF547D
    CF538H
    CF516D
    CF505E
  • 原文地址:https://www.cnblogs.com/--560/p/4330265.html
Copyright © 2011-2022 走看看