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\n", res);
    }

    return 0;
}

 

 

posted on 2013-08-03 10:14  Still_Raining  阅读(426)  评论(0编辑  收藏  举报