zoukankan      html  css  js  c++  java
  • hdu 3549 Flow Problem

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 

    Flow Problem

    Description

    Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.

    Input

    The first line of input contains an integer T, denoting the number of test cases.
    For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
    Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)

    Output

    For each test cases, you should output the maximum flow from source 1 to sink N.

    Sample Input

    2
    3 2
    1 2 1
    2 3 1
    3 3
    1 2 1
    2 3 1
    1 3 1

    Sample Output

    Case 1: 1
    Case 2: 2

    裸的最大流dinic,测模板。。

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<map>
    using std::min;
    using std::find;
    using std::sort;
    using std::pair;
    using std::queue;
    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 = 1100;
    const int INF = 0x3f3f3f3f;
    struct Dinic {
        struct edge { int to, cap, next, rev; }G[N << 2];
        int s, t, tot, level[N], ite[N], 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 n, int m) {
            int u, v, f;
            s = 1, t = n;
            while(m--) {
                scanf("%d %d %d", &u, &v, &f);
                add_edge(u, v, f);
            }
        }
        inline void bfs(int s) {
            cls(level, -1);
            queue<int> q;
            q.push(s);
            level[s] = 0;
            while(!q.empty()) {
                int u = q.front(); q.pop();
                for(int i = head[u]; ~i; i = G[i].next) {
                    edge &e = G[i];
                    if(e.cap > 0 && level[e.to] < 0) {
                        level[e.to] = level[u] + 1;
                        q.push(e.to);
                    }
                }
            }
        }
        inline int dfs(int u, int t, int f) {
            if(u == t) return f;
            for(int &i = ite[u]; ~i; i = G[i].next) {
                edge &e = G[i];
                if(e.cap > 0 && level[u] < level[e.to]) {
                    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 int max_flow() {
            int flow = 0;
            while(true) {
                bfs(s);
                if(level[t] < 0) break;
                int f;
                rep(i, t) ite[i] = head[i];
                while((f = dfs(s, t, INF)) > 0) {
                    flow += f;
                }
            }
            return flow;
        }
        inline void solve(int n, int m) {
            static int k = 1;
            init(), built(n, m);
            printf("Case %d: %d
    ", k++, max_flow());
        }
    }go;
    int main() {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w+", stdout);
    #endif
        int t, n, m;
        scanf("%d", &t);
        while(t--) {
            scanf("%d %d", &n, &m);
            go.solve(n, m);
        }
        return 0;
    }
  • 相关阅读:
    C#.NET中的ToString()数字格式化
    Entity Framework实现属性映射约定
    CentOS6.3安装MySQL5.5
    Xshell传输文件
    centos直接yum安装nginx
    winform 开发中 把耗时操作 封装起来 异步执行(.net 4.0)
    c# action<> func<> 这2个委托怎么用和理解
    git@oschina使用入门(图形界面版)
    Linux任务前后台的切换
    linux 常用命令,开发记住这些基本能够玩转linux
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4792931.html
Copyright © 2011-2022 走看看