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;
    }
    
  • 相关阅读:
    Effective.Java第67-77条(异常相关)
    淘宝店的图片哪里来的
    SEO高手在扯蛋?
    多少人,没能熬过那最初的三厘米!
    网站要提高权重要总结三点
    真正的干货是什么?
    google打不开怎么办?谷歌打不开的解决方法
    StatCounter
    教你如何抢注一个快过期的域名
    正确识别希捷Backup Plus新睿品1TB正品,杜绝奸商猖獗
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/9799912.html
Copyright © 2011-2022 走看看