hdu 3459 Flow Problem

最大流裸题,有向图,然后又重边,容量要累加,这里是用BFS来做的

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define    N 20
queue<int>q;
int f[N][N],c[N][N],a[N],p[N],flow;
int n,m;
//源点和汇点分别规定为1,n
void BFS()
{
    flow=0;
    while(1)
    {
        memset(a,0,sizeof(a));
        a[1]=INF;
        while(!q.empty()) q.pop();
        q.push(1);
        while(!q.empty())
        {
            int u;
            u=q.front();  q.pop();
            for(int v=1; v<=n; v++)
                if(!a[v] && c[u][v]>f[u][v])
                {
                    p[v]=u; 
                    q.push(v);
                    a[v]=a[u]<c[u][v]-f[u][v] ? a[u] : c[u][v]-f[u][v];
                }
        }
        if(!a[n]) break;
        for(int u=n; u!=1; u=p[u])
        {
            f[p[u]][u]+=a[n];
            f[u][p[u]]-=a[n];
        }
        flow+=a[n];
    }
    return ;
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int t=1; t<=T; t++)
    {
        scanf("%d%d",&n,&m);
        memset(f,0,sizeof(f));
        memset(c,0,sizeof(c));
        memset(p,0,sizeof(p));
        for(int i=1; i<=m; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            c[u][v]+=w;
        }
        BFS();
        printf("Case %d: %d\n",t,flow);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/scau20110726/p/2783507.html