hihocoder 1378(有向图求最小割集)

在最大流模板的基础上,求出最小割集

题目链接:hihocoder 1378

#include
#include
#include
#include 
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define maxn 20005
#define maxm 100005
#define rd(x) scanf("%d", &x)
#define rd2(x, y) scanf("%d%d", &x, &y)

const int INF = 0x3f3f3f3f;
struct Edge{
    int to, next, cap, flow;
}edge[maxm];
int tol, head[maxn];
int gap[maxn], dep[maxn], pre[maxn], cur[maxn];
void init(){
    tol = 0;
    memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w, int rw = 0){
    edge[tol].to = v; edge[tol].cap = w; edge[tol].next = head[u];
    edge[tol].flow = 0; head[u] = tol++;
    edge[tol].to = u; edge[tol].cap = rw; edge[tol].next = head[v];
    edge[tol].flow = 0; head[v] = tol++;
}
int sap(int st, int ed, int N){
    memset(gap, 0 ,sizeof(gap));
    memset(dep, 0 ,sizeof(dep));
    memcpy(cur, head, sizeof(head));
    int u = st;
    pre[u] = -1;
    gap[0] = N;
    int ans = 0;
    while(dep[st] < N){
        if(u == ed){
            int Min = INF;
            for(int i = pre[u]; i != -1; i = pre[edge[i^1].to])
                if(Min > edge[i].cap - edge[i].flow)
                    Min = edge[i].cap - edge[i].flow;
            for(int i = pre[u]; i != -1; i = pre[edge[i^1].to]){
                edge[i].flow += Min;
                edge[i^1].flow -= Min;
            }
            u = st;
            ans += Min;
            continue;
        }
        bool flag = false;
        int v;
        for(int i = cur[u]; i != -1; i = edge[i].next){
            v = edge[i].to;
            if(edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u]){
                flag = true;
                cur[u] = pre[v] = i;
                break;
            }
        }
        if(flag){
            u = v;
            continue;
        }
        int Min = N;
        for(int i = head[u]; i!= -1; i = edge[i].next){
            if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min){
                Min = dep[edge[i].to];
                cur[u] = i;
            }
        }
        gap[dep[u]]--;
        if(!gap[dep[u]]) return ans;
        dep[u] = Min + 1;
        gap[dep[u]]++;
        if(u != st) u = edge[pre[u]^1].to;
    }
    return ans;
}
/*求最小割集*/
set sset;
int lvl[maxn];
void bfs(){
    queue q;
    memset(lvl, 0 ,sizeof(lvl));
    q.push(1);
    lvl[1] = 1;
    while(!q.empty()){
        int u = q.front();
        sset.insert(u);
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            int v = edge[i].to;
            if(lvl[v] == 0 && edge[i].cap > edge[i].flow){
                lvl[v] = lvl[u] + 1;
                /*这里edge[i]就是最小割里的边了*/
                q.push(v);
            }
        }
    }
}

int main()
{
    int n, m, a, b, c;
    init();
    rd2(n, m);
    for(int i = 1; i <= m; i++){
        scanf("%d%d%d", &a, &b, &c);
        addedge(a,b,c);
    }
    int maxflow = sap(1, n, n);
    bfs();
    printf("%d %d\n", maxflow, sset.size());
    for(set::iterator it = sset.begin(); it != sset.end(); it++){
        if(it != sset.begin()) printf(" ");
        printf("%d", *it);
    }
    printf("\n");
    return 0;
}


你可能感兴趣的:(图)