LouZhang

导航

网络流sap需要注意的地方

int sap(){
    memset(level, 0, sizeof level);
    memset(gap, 0, sizeof gap);
    memset(cur, 0, sizeof cur);
    int u = pre[s] = s;
    int aug = inf;
    gap[s] = n;这个要使源点gap值为点个数
    int v;
    int flow = 0;
    while(level[s] < n){
        for(v = cur[u]; v < n; v ++){
            if(c[u][v] > 0 && level[u] == level[v] + 1){
                break;
            }
        }
        if(v < n){
            pre[v] = u;
            if(aug > c[u][v]) aug = c[u][v];
            u = cur[u] = v;
            if(u == t){
                flow += aug;
                for(v = t; v != s; v = pre[v]){
                    if(c[pre[v]][v] != inf)
                      c[pre[v]][v] -= aug;
                    if(c[v][pre[v]] != inf)
                      c[v][pre[v]] += aug;
                }
                aug = inf, u = s;
            }
        }else{
            int min_label = t;这个要从t开始,因为如果都不满足的话min_label就是t了,也就是最大
            for(v = 0; v < n; v ++){
                if(c[u][v] > 0 && min_label > level[v]){
                    cur[u] = v;
                    min_label = level[v];
                }
            }
            if(--gap[level[u]] == 0) return flow;
            level[u] = min_label + 1;
            gap[level[u]] ++;
            u = pre[u];
        }
    }
    return flow;
}

posted on 2012-08-17 15:50  louzhang_swk  阅读(257)  评论(0编辑  收藏  举报