3549Flow Problem

[cce_cpp]
//网络流,基本是照着书上的代码敲的,还敲错了o(╯□╰)o
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
int cap[20][20],flow[20][20],a[20],p[20];
const int INF = 1000000;
int main()
{
    int t,n,m,x,y,c,f;
    scanf("%d",&t);
    for(int i = 1; i <= t; ++i)
    {
        scanf("%d%d",&n,&m);
        queue<int> q;
        f = 0;
        memset(flow,0,sizeof(flow));
        memset(cap,0,sizeof(cap));
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&c);
            cap[x][y] += c;
        }
        for(;;)
        {
            memset(a,0,sizeof(a));
            a[1] = INF;
            q.push(1);
            while(!q.empty())
            {
                int u = q.front();
                q.pop();
//在某次传递中,如果a[v]点未访问且容量大于流量,则从u点传到v点,记录v点的parent是u
                for(int v = 1; v <= n; ++v)if(!a[v] && cap[u][v] > flow[u][v]) 
   
                {
                    p[v] = u;
                    q.push(v);
                    a[v] = min(a[u], cap[u][v] - flow[u][v]);
                }
            }
            if(a[n]==0)
            break;
            for(int u= n; u != 1; u = p[u])
            {
                flow[p[u]][u] += a[n];
                flow[u][p[u]] -= a[n];//可能往回传
            }
            f += a[n];
        }
        printf("Case %d: %d\n",i,f);
    } 
    return 0;
}
[/cce_cpp]
原文地址:https://www.cnblogs.com/fchx/p/3097580.html