zoukankan      html  css  js  c++  java
  • 洛谷P2740 草地排水

    最大流

    一道完全符合最大流定义的板子题。。重新学了一次网络流,希望有更深的理解把。。

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const int N = 205;
    int n, m, cnt, head[N], depth[N];
    struct Edge{ int v, next, w; } edge[N<<2];
    
    void addEdge(int a, int b, int w){
        edge[cnt].v = b, edge[cnt].w = w, edge[cnt].next = head[a], head[a] = cnt ++;
        edge[cnt].v = a, edge[cnt].w = 0, edge[cnt].next = head[b], head[b] = cnt ++;
    }
    
    bool bfs(){
        full(depth, 0);
        queue<int> q;
        q.push(1);
        depth[1] = 1;
        while(!q.empty()){
            int s = q.front(); q.pop();
            for(int i = head[s]; i != -1; i = edge[i].next){
                int u = edge[i].v;
                if(!depth[u] && edge[i].w > 0){
                    depth[u] = depth[s] + 1;
                    q.push(u);
                }
            }
        }
        return depth[m] != 0;
    }
    
    int dfs(int s, int a){
        if(s == m) return a;
        int flow = 0;
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(depth[u] == depth[s] + 1 && edge[i].w > 0){
                int k = dfs(u, min(a, edge[i].w));
                flow += k, a -= k, edge[i].w -= k, edge[i^1].w += k;
            }
            if(!a) break;
        }
        if(a) depth[s] = -1;
        return flow;
    }
    
    int dinic(){
        int ans = 0;
        while(bfs()){
            ans += dfs(1, INF);
        }
        return ans;
    }
    
    int main(){
    
        full(head, -1);
        n = read(), m = read();
        for(int i = 0; i < n; i ++){
            int u = read(), v = read(), w = read();
            addEdge(u, v, w);
        }
        printf("%d
    ", dinic());
        return 0;
    }
    
  • 相关阅读:
    Dubbo简介
    Centos之关机和重启命令
    VirtualBox中CentOS7.2 网络配置(固定IP+联网)
    c#Post方法封装处理
    C# 异步方法处理
    将XMLrequest 改写成fetch
    AsyncCallback
    Promise
    FETCH
    HTML DOM Event 对象
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10662919.html
Copyright © 2011-2022 走看看