• HDU3549 Flow Problem(网络流增广路算法)


    题目链接

    分析:

    网络流增广路算法模板题。http://www.cnblogs.com/tanhehe/p/3234248.html

    AC代码:

    #include <iostream>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    const int maxn = 20;
    const int INF = (1<<30);
    
    int cap[maxn][maxn], flow[maxn][maxn];
    int n;
    
    int EdmondsKarp(int s, int t) {
        int p[maxn], a[maxn];
        queue<int> q;
    
        memset(flow, 0, sizeof(flow));
        int f = 0;
    
        while(true) {
            memset(a, 0, sizeof(a));
    
            a[s] = INF;
    
            q.push(s);
    
            while(!q.empty()) { //BFS 找增广路
                int u = q.front(); q.pop();
    
                for(int v=1; v<=n; v++) if(!a[v] && cap[u][v]>flow[u][v]){
                    //找到新节点v
                    p[v] = u; q.push(v);
                    a[v] = min(a[u], cap[u][v]-flow[u][v]);
                }
            }
    
            if(a[t] == 0) break;    //找不到,则当前流已经是最大流
    
            for(int u=t; u != s; u = p[u]) {    //从汇点往回走
                flow[p[u]][u] += a[t];  //更新正向流量
                flow[u][p[u]] -= a[t];  //更新反向流量
            }
    
            f += a[t];  //更新从 s 流出的流量
        }
    
        return f;
    }
    
    int main(){
        int T, m, u, v, c;
    
        scanf("%d", &T);
    
        for(int kase = 1; kase <= T; kase++) {
            scanf("%d%d", &n, &m);
    
            memset(cap, 0, sizeof(cap));
    
            for(int i=0; i<m; i++) {
                scanf("%d %d %d", &u, &v, &c);
                cap[u][v] += c;
            }
    
            printf("Case %d: ", kase);
            int res = EdmondsKarp(1, n);
    
            printf("%d
    ", res);
        }
    
        return 0;
    }
  • 相关阅读:
    eyou通用标签的调取
    eyou头部相关标签的调用
    自增标签循环+1的方法
    文章内容页相关的标签
    当前栏目有多少文章
    指定栏目最顶级栏目名称
    当前单页正文
    Python-pandas常用函数
    监控在线平台
    网页爬虫---音乐
  • 原文地址:https://www.cnblogs.com/tanhehe/p/3234262.html
Copyright © 2020-2023  润新知