SGU - 176 Flow construction(上下界有源有汇最小流)

题目大意:给出N个点,M条有向边
如果有向边的标号是1的话,就表示该边的上界下界都为容量
如果有向边的标号为0的哈,表示该边的下界为0,上界为容量
现在问,从1到N的最小流是多少,并输出每条边的流量

解题思路:上下界有源有汇最小流,基本模版

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXNODE = 210;
const int MAXEDGE = 100010;
typedef int Type;
const Type INF = 0x3f3f3f3f;

struct Edge{
    int u, v, next, id;
    Type cap, flow;
    Edge() {}
    Edge(int u, int v, Type cap, Type flow, int next, int id) : u(u), v(v), cap(cap), flow(flow), next(next), id(id){}
};

struct Dinic{
    int n, m, s, t;
    Edge edges[MAXEDGE];
    int head[MAXNODE];
    int cur[MAXNODE];
    bool vis[MAXNODE];
    Type d[MAXNODE];
    vector<int> cut;

    void init(int n) {
        this->n = n;
        memset(head, -1, sizeof(head));
        m = 0;
    }

    void AddEdge(int u, int v, Type cap, int id) {
        edges[m] = Edge(u, v, cap, 0, head[u], id);
        head[u] = m++;
        edges[m] = Edge(v, u, 0, 0, head[v], id);
        head[v] = m++;
    //  printf("u is %d v is %d cap is %d\n", u, v, cap);
    } 

    bool BFS() {
        memset(vis, 0, sizeof(vis));
        queue<int> Q;
        Q.push(s);
        d[s] = 0;
        vis[s] = 1;

        while (!Q.empty()) {
            int u = Q.front(); Q.pop();
            for (int i = head[u]; ~i; i = edges[i].next) {
                Edge &e = edges[i];
                if (!vis[e.v] && e.cap > e.flow) {
                    vis[e.v] = true;
                    d[e.v] = d[u] + 1;
                    Q.push(e.v);
                }
            }
        }
        return vis[t];
    }

    Type DFS(int u, Type a) {
        if (u == t || a == 0) return a;

        Type flow = 0, f;
        for (int &i = cur[u]; i != -1; i = edges[i].next) {
            Edge &e = edges[i];
            if (d[u] + 1 == d[e.v] && (f = DFS(e.v, min(a, e.cap - e.flow))) > 0) {
                e.flow += f;
                edges[i ^ 1].flow -= f;
                flow += f;
                a -= f;
                if (a == 0) break;
            }
        }
        return flow;
    }

    Type Maxflow(int s, int t) {
        this->s = s; this->t = t;
        Type flow = 0;
        while (BFS()) {
            for (int i = 0; i < n; i++)
                cur[i] = head[i];
            flow += DFS(s, INF);
        }
        return flow;
    }

    void Mincut() {
        cut.clear();
        for (int i = 0; i < m; i += 2) {
            if (vis[edges[i].u] && !vis[edges[i].v]) 
                cut.push_back(i);
        }
    }

    bool judge() {
        for (int i = head[s]; ~i; i = edges[i].next)
            if (edges[i].cap - edges[i].flow != 0) return false;
        return true;
    }
}dinic;

#define maxn 110
#define maxm 10010
int n, m;
int d[maxn], ans[maxm];

void init() {
    int source = n + 1, sink = n + 2;
    dinic.init(sink + 1);
    memset(d, 0, sizeof(d));
    memset(ans, 0, sizeof(ans));

    int u, v, c, f;
    for (int i = 1; i <= m; i++) {
        scanf("%d%d%d%d", &u, &v, &c, &f);
        if (f) {
            d[u] -= c; d[v] += c; ans[i] = c;
        }
        else dinic.AddEdge(u, v, c, i);
    }

    int tot = dinic.m;
    for (int i = 1; i <= n; i++) {
        if (d[i] > 0) dinic.AddEdge(source, i, d[i], 0);
        if (d[i] < 0) dinic.AddEdge(i, sink, -d[i], 0);
    }

    dinic.Maxflow(source, sink);
    dinic.AddEdge(n, 1, INF, 0);
    dinic.Maxflow(source, sink);
    if (!dinic.judge()) printf("Impossible\n");
    else {
        printf("%d\n", dinic.edges[dinic.m - 2].flow);
        for (int i = 0; i < tot; i += 2)
            ans[dinic.edges[i].id] += dinic.edges[i].flow;
        for (int i = 1; i <= m; i++)
            if (i ^ m)
                printf("%d ", ans[i]);
            else
                printf("%d\n", ans[i]);
    }
}

int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        init();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值