zoukankan      html  css  js  c++  java
  • [洛谷P3376]【模板】网络最大流(ISAP)

    C++ Code:(ISAP)

    #include <cstdio>
    #include <cstring>
    #define maxn 1210
    #define maxm 120010
    int n, m, st, ed;
    const int inf = 0x7fffffff;
    inline int min(int a, int b) {return a < b ? a : b;}
    namespace Network_Flow {
        int st, ed, MF, n;
        int head[maxn], cnt = 2;
        struct Edge {
            int to, nxt, w;
        } e[maxm << 1];
        inline void addE(int a, int b, int c) {
            e[cnt] = (Edge) {b, head[a], c}; head[a] = cnt;
            e[cnt ^ 1] = (Edge) {a, head[b], 0}; head[b] = cnt ^ 1;
            cnt += 2;
        }
        int GAP[maxn], d[maxn];
        int last[maxn];
        int q[maxn], h, t;
        inline void init() {
            GAP[d[ed] = 1] = 1;
            for (int i = 1; i <= n; i++) last[i] = head[i]; 
            q[h = t = 0] = ed;
            while (h <= t) {
                int u = q[h++];
                for (int i = head[u]; i; i = e[i].nxt) {
                    int v = e[i].to;
                    if (!d[v]) {
                        d[v] = d[u] + 1;
                        GAP[d[v]]++;
                        q[++t] = v;
                    }
                }
            }
        }
        int dfs(int u, int low) {
            if (!low || u == ed) return low;
            int w, res = 0;
            for (int &i = last[u]; i; i = e[i].nxt) {
                int v = e[i].to;
                if (d[u] == d[v] + 1) {
                    w = dfs(v, min(low, e[i].w));
                    res += w, low -= w;
                    e[i].w -= w, e[i ^ 1].w += w;
                    if (!low) return res;
                }
            }
            if (!(--GAP[d[u]])) d[st] = n + 1;
            ++GAP[++d[u]], last[u] = head[u];
            return res;
        }
        inline void ISAP(int S, int T) {
            st = S, ed = T;
            init();
            while (d[st] <= n) MF += dfs(st, inf);
        }
        inline void clear() {
            memset(head, 0, sizeof head); cnt = 2;
            memset(GAP, 0, sizeof GAP);
            memset(d, 0, sizeof d);
            MF = 0;
        }
    }
    #define read() R::READ()
    #include <cctype>
    namespace R {
        int x;
        #ifdef ONLINE_JUGER
        #define M 1 << 25
        char op[M], *ch;
        inline void init() {
            fread(ch = op, 1, M, stdin);
        }
        inline int READ() {
            while (isspace(*ch)) ch++;
            for (x = *ch & 15, ch++; isdigit(*ch); ch++) x = x * 10 + (*ch & 15);
            return x;
        }
        #undef M
        #else
        char ch;
        inline int READ() {
            ch = getchar();
            while (isspace(ch)) ch = getchar();
            for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
            return x;
        }
        #endif
    }
    int main() {
        #ifdef ONLINE_JUGER
        R::init();
        #endif
        n = read(), m = read(), st = read(), ed = read();
        Network_Flow::n = n;
        for (int i = 0, a, b, c; i < m; i++) {
            a = read(), b = read(), c = read();
            Network_Flow::addE(a, b, c);
        }
        Network_Flow::ISAP(st, ed);
        printf("%d
    ", Network_Flow::MF);
        return 0;
    }
    
  • 相关阅读:
    Rose2003执行出现 -2147417848 (80010108)&#39;:Automation 错误
    Xcode 证书生成、设置、应用
    关于数组中加入相同的view的试验
    XCode 项目配置说明
    ios申请真机调试( xcode 5)详细解析
    CAShapeLayer--备用
    手把手教你 iOS通过自己的服务器实现应用分发
    iOS最新上线流程+续费 2015-7-20更新
    iOS8的一些控件的变更
    iOS8上放大缩小的动画
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/9799912.html
Copyright © 2011-2022 走看看