zoukankan      html  css  js  c++  java
  • Net FLow Template

    EK  Template :

    bool bfs(int src, int des){
        memset(pre, -1, sizeof(pre));
        while(!que.empty()) que.pop();
        pre[src] = 0;
        int index;
        que.push(src);
        while(!que.empty()){
            index = que.front();
            que.pop();
            for(int i = src; i <= des; ++i){
                if(pre[i] == -1 && map[index][i] > 0){
                    pre[i] = index;
                    if(i == des)    return true;
                    que.push(i);
                }
            }
        }
        return false;
    }
    
    int MaxFlow(int src, int des){
        int maxflow = 0;
        while(bfs(src, des)){
            int minflow = INF;
            int i;
            for(i = des; i != src; i = pre[i])  
                minflow = min(minflow, map[pre[i]][i]);
            for(i = des; i != src; i = pre[i]){
                map[pre[i]][i] -= minflow;
                map[i][pre[i]] += minflow;
            }
            maxflow += minflow;
        }
        return maxflow;
    }

    SAP + GAP  Template :

    int sap(int u,int flow){
        if(u == src) return flow;
        int ans = 0, i, t;
        for(i = src; i <= des; ++i)
            if(map[u][i] && dis[u] == dis[i] + 1){
                t = sap(i, min(flow - ans, map[u][i]));
                map[u][i] -= t, map[i][u] += t,ans += t;
                if(ans == flow) return ans;
            }
        if(dis[src] >= n + 2) return ans;
        if(!--gap[dis[u]]) dis[src] = n + 2;
        ++gap[++dis[u]];
        return ans;
    }
    	src = 1, des = n;
    	for(gap[0] = n + 2; dis[src] < n + 2; )
            ans += sap(src,INF);
    

      


  • 相关阅读:
    mybatis-cache model
    多线程开发(1)
    正则表达式(3) — 正则表达式库
    正则表达式(2) — 表达式规则
    我在迈瑞工作的两年总结
    正则表达式(1) — 常用的表达式
    C++系列(2) — 智能指针
    C++系列(1) — string
    路径去除前缀
    SIMD性能优化
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/3905529.html
Copyright © 2011-2022 走看看