ZOJ 1324 Reactor Cooling——无源汇有上下界的可行流

来源:互联网 发布:sql删除完全重复数据 编辑:程序博客网 时间:2024/06/04 20:04
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>using namespace std;const int maxn = 410;const int INF = 0x3f3f3f3f;int a[maxn], down[maxn*maxn];struct Edge {    int from, to, flow, cap;    Edge(int a, int b, int c, int d) : from(a), to(b), flow(c), cap(d) {}};struct Dinic {    bool vis[maxn];    int n, m, s, t, d[maxn], cur[maxn];    vector<Edge> edges;    vector<int> G[maxn];    void init(int nn) {        n = nn;        edges.clear();        for (int i = 0; i <= n + 5; i++) G[i].clear();    }    void addedge(int from, int to, int flow, int cap) {        edges.push_back(Edge(from, to, flow, cap));        edges.push_back(Edge(to, from, -flow, 0));        m = edges.size();        G[from].push_back(m-2);        G[to].push_back(m-1);    }    bool bfs() {        memset(vis, 0, sizeof(vis));        queue<int> Q; Q.push(s);        d[s] = 0;        vis[s] = 1;        while (!Q.empty()) {            int x = Q.front(); Q.pop();            for (int i = 0; i < G[x].size(); i++) {                Edge &e = edges[G[x][i]];                if (!vis[e.to] && e.cap > e.flow) {                    vis[e.to] = 1;                    d[e.to] = d[x] + 1;                    Q.push(e.to);                }            }        }        return vis[t];    }    int dfs(int x, int a) {        if (x == t || a == 0) return a;        int flow = 0, f;        for (int &i = cur[x]; i < G[x].size(); i++) {            Edge &e = edges[G[x][i]];            if (d[x] + 1 == d[e.to] && (f=dfs(e.to, min(a, e.cap - e.flow))) > 0) {                e.flow += f;                edges[G[x][i]^1].flow -= f;                flow += f;                a -= f;                if (a == 0) break;            }        }        return flow;    }    int maxflow(int ss, int tt) {        s = ss, t = tt;        int flow = 0;        while (bfs()) {            memset(cur, 0, sizeof(cur));            flow += dfs(s, INF);        }        return flow;    }    void output(int m) {        for (int i = 1; i <= m; i++) {            printf("%d\n", down[i] + edges[(i-1)*2].flow);        }    }}dinic;int main() {    int T, n, m, from, to, up;    scanf("%d", &T);    for (int kase = 1; kase <= T; kase++) {        scanf("%d %d", &n, &m);        dinic.init(n);        memset(a, 0, sizeof(a));        for (int i = 1; i <= m; i++) {            scanf("%d %d %d %d", &from, &to, &down[i], &up);            dinic.addedge(from, to, 0, up - down[i]);            a[from] -= down[i];            a[to] += down[i];        }        int ss = n + 1, tt = n + 2, sum = 0;        for (int i = 1; i <= n; i++) {            if (a[i] > 0) {                dinic.addedge(ss, i, 0, a[i]);            }            else {                sum -= a[i];                dinic.addedge(i, tt, 0, -a[i]);            }        }        if (kase != 1) printf("\n");        int temp = dinic.maxflow(ss, tt);        if (temp != sum) {            printf("NO\n");        }        else {            printf("YES\n");            dinic.output(m);        }    }    return 0;}

阅读全文
0 0