网络流入门题hdu3549(Flow Problem)

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 

题意就是给你一个图,求从1到n的最大流,因为今天只学会了ford算法,所以也就使用ford算法完成了,ford算法的复杂度为n*m*U,结点个数*边数*最大的弧容量,本题n=15,m=1000,U=1000,好吧,第一眼就觉得有很多重边,如果从i到j有多条路,那么i->j的容量应该是所有路容量之和

ford算法是利用增广路来求解最大流的,增广路又称可改进路,对于一条从1到n的链来说,如果前向边全都不饱和,后向边都不为零流,则一定能找到一个值a,使所有的前向边流量+a,所有的后向边减a,且加法后当前流量不超过容量也不低于0,进行这样的操作后,送往终点的流量一定会增加,重复操作指导无法找到下一条增广路为止,接着统计出发点的所有流量之和,极为此图的最大流。

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int MAXN = 20;
const int INF = 0x3f3f3f3f;
struct Node {
    int c, v;
};
Node mp[MAXN][MAXN];
int n, m;
int pre[MAXN];
queue<int> q;
bool vis[MAXN];

int bfs()
{
    while( !q.empty() ) q.pop();
    memset( vis, 0, sizeof( vis ) );
    q.push( 1 );
    vis[1] = 1;
    pre[1] = 1;
    int Min = INF;
    bool flag = false;
    while( !q.empty() ) {
        int t = q.front();
        q.pop();
        if( t == n ) {
            flag = true;
            break;
        }
        for( int k = 1; k <= n; k++ ) {
            if( vis[k] ) continue;
            if( mp[t][k].c < INF && mp[t][k].v < mp[t][k].c ) {
                q.push( k );
                pre[k] = t;
                vis[k] = 1;
                Min = min( Min, mp[t][k].c - mp[t][k].v );
            } else if( mp[k][t].c < INF && mp[k][t].v > 0 ) {
                q.push( k );
                pre[k] = -t;
                vis[k] = 1;
                Min = min( Min, mp[k][t].v );
            }
        }
    }
    if( !flag ) return -1;
    return Min;
}

int ford()
{
    while( 1 ) {
        int a = bfs();
        if( a == -1 ) break;
        int k1 = pre[n], k2 = n;
        while( 1 ) {
            if( k1 > 0 ) {
                mp[k1][k2].v += a;
                k2 = k1;
                k1 = pre[k1];
            } else if( k1 < 0 ) {
                k1 = -k1;
                mp[k2][k1].v -= a;
                k2 = k1 ;
                k1 = pre[k1];
            }
            if( k1 == k2 ) break;
        }
    }
    int ans = 0;
    for( int k = 2; k <= n; k++ ) {
        if( mp[1][k].c < INF )
            ans += mp[1][k].v;
    }
    return ans;
}
int main()
{
#ifdef LOCAL_DEBUG
    freopen( "input.txt", "r", stdin );
#endif // LOCAL_DEBUG
    int T, cas = 1;
    scanf( "%d", &T );
    while( T-- ) {
        printf( "Case %d: ", cas++ );
        scanf( "%d%d", &n, &m );
        int from, to, flow;
        for( int i = 1; i <= n; i++ )
            for( int j = 1; j <= n; j++ )
                mp[i][j].c = mp[i][j].v = INF;
        for( int i = 1; i <= m; i++ ) {
            scanf( "%d%d%d", &from, &to, &flow );
            if( mp[from][to].c == INF )
                mp[from][to].c = flow;
            else
                mp[from][to].c += flow;
            mp[from][to].v = 0;
        }
        int ans = ford();
        printf( "%d\n", ans );
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值