zoukankan      html  css  js  c++  java
  • BZOJ 1877: [SDOI2009]晨跑

    二次联通门 : BZOJ 1877: [SDOI2009]晨跑

    /*
        BZOJ 1877: [SDOI2009]晨跑
    
        拆点 + 费用流
    
    */
    #include <cstdio>
    #include <iostream>
    #define rg register
    inline void read (int &n) {
        rg char c = getchar ();
        for (n = 0; !isdigit (c); c = getchar ());
        for (; isdigit (c); n = n * 10 + c - '0', c = getchar ());
    }
    int S, T;
    #define Max 7000
    #define INF 1e9
    namespace net {
        const int MaxE = 2000000;
        int _n[MaxE], _v[MaxE], list[Max], _f[MaxE], _c[MaxE], EC = 1, d[Max], q[MaxE], pre[Max];
        bool is[Max]; int c[Max];
        inline void In (int u, int v, int f, int c) { 
            _v[++ EC] = v, _n[EC] = list[u], list[u] = EC, _f[EC] = f, _c[EC] = c;
            _v[++ EC] = u, _n[EC] = list[v], list[v] = EC, _f[EC] = 0, _c[EC] = -c;
        }
    
        bool Bfs () { 
            int h = 1, t = 1; q[t] = S; rg int i, n;
            for (i = 0; i <= T; ++ i) d[i] = INF, is[i] = false;
            for (d[S] = 0, c[S] = INF, pre[S] = 0; h <= t; ++ h)
                for (n = q[h], is[n] = false, i = list[n]; i; i = _n[i]) 
                    if (d[_v[i]] > d[n] + _c[i] && _f[i]) {
                        d[_v[i]] = d[n] + _c[i], pre[_v[i]] = i, c[_v[i]] = std :: min (c[n], _f[i]);
                        if (!is[_v[i]]) q[++ t] = _v[i], is[_v[i]] = true; 
                    }
            return d[T] < INF;
        }
        int Do () { 
            int res = 0, p = 0; rg int i;
            for (int x; Bfs (); ++ p) {
                for (x = c[T], i = T; i != S; i = _v[pre[i] ^ 1])
                    _f[pre[i]] -= x, _f[pre[i] ^ 1] += x;
                res += d[T] * x;
            }
            printf ("%d %d", p, res);
        }
    }
    int main (int argc, char *argv[]) { 
        int N, M; read (N), read (M); rg int i;
        S = 1, T = N << 1; int x, y, z;
        net :: In (S, S + N, INF, 0), net :: In (N, T, INF, 0);
        for (i = 1; i <= M; ++ i) read (x), read (y), read (z), net :: In (N + x, y, 1, z);
        for (i = 2; i < N; ++ i) net :: In (i, i + N, 1, 0);
        net :: Do ();
        return 0;
    }
  • 相关阅读:
    Crawling Computing Ranking 很长时间, 怎么办?
    明月外,净红尘
    数据库人员面试:SQL Server常用测试题
    华山人物志——苏颖超
    安全性和 XML Web services
    聚簇索引与非聚簇索引的区别以及SQL Server查询优化技术
    SQL2000里的数据类型
    [精华] 数据库的查询优化技术
    深入浅出理解索引结构
    XSL
  • 原文地址:https://www.cnblogs.com/ZlycerQan/p/8318867.html
Copyright © 2011-2022 走看看