码迷,mamicode.com
首页 > Web开发 > 详细

Net FLow Template

时间:2014-08-11 21:15:52      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   strong   for   ar   div   

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);

  


 

Net FLow Template,布布扣,bubuko.com

Net FLow Template

标签:des   style   blog   color   strong   for   ar   div   

原文地址:http://www.cnblogs.com/wushuaiyi/p/3905529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!