zoukankan      html  css  js  c++  java
  • poj 1273 Drainage Ditches

    题目连接

    http://poj.org/problem?id=1273 

    Drainage Ditches

    Description

    Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
    Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
    Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

    Input

    The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

    Output

    For each case, output a single integer, the maximum rate at which water may emptied from the pond.

    Sample Input

    5 4
    1 2 40
    1 4 20
    2 4 20
    2 3 30
    3 4 10

    Sample Output

    50

    最大流模板题。。

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<map>
    using std::min;
    using std::find;
    using std::sort;
    using std::pair;
    using std::vector;
    using std::multimap;
    #define pb(e) push_back(e)
    #define sz(c) (int)(c).size()
    #define mp(a, b) make_pair(a, b)
    #define all(c) (c).begin(), (c).end()
    #define iter(c) __typeof((c).begin())
    #define cls(arr, val) memset(arr, val, sizeof(arr))
    #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    #define rep(i, n) for(int i = 0; i < (int)n; i++)
    #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
    const int N = 210;
    const int INF = 0x3f3f3f3f;
    struct Ford_Flukerson {
        struct edge { int to, cap, next, rev; }G[N << 1];
        bool vis[N];
        int tot, head[N];
        inline void init() {
            tot = 0, cls(head, -1);
        }
        inline void add_edge(int u, int v, int cap) {
            G[tot] = (edge){ v, cap, head[u], tot + 1 }; head[u] = tot++;
            G[tot] = (edge){ u,   0, head[v], tot - 1 }; head[v] = tot++;
        }
        inline void built(int m) {
            int u, v, f;
            while(m--) {
                scanf("%d %d %d", &u, &v, &f);
                add_edge(u, v, f);
            }
        }
        inline int dfs(int u, int t, int f) {
            if(u == t) return f;
            vis[u] = true;
            for(int i = head[u]; ~i; i = G[i].next) {
                edge &e = G[i];
                if(!vis[e.to] && e.cap > 0) {
                    int d = dfs(e.to, t, min(e.cap, f));
                    if(d > 0 ) {
                        e.cap -= d;
                        G[e.rev].cap += d;
                        return d;
                    }
                }
            }
            return 0;
        }
        inline void max_flow(int n) {
            int flow = 0;
            while(true) {
                cls(vis, false);
                int f = dfs(1, n, INF);
                if(!f) break;
                flow += f;
            }
            printf("%d
    ", flow);
        }
    }go;
    int main() {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w+", stdout);
    #endif
        int n, m;
        while(~scanf("%d %d", &m, &n)) {
            go.init();
            go.built(m);
            go.max_flow(n);
        }
        return 0;
    }
  • 相关阅读:
    Websocket基础知识简记
    jmeter websocket接口测试
    软件测试的艺术 笔记(上)
    错误提示Unable to preventDefault inside passive event listener解决方法
    vue-cil3关闭eslint语法检查
    mongoDB无法启动服务器
    Vue之todoList
    react踩坑第一章
    父组件向孙子组件传值(Context)特性
    变量声明
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4773024.html
Copyright © 2011-2022 走看看