HDU 3549:Flow Problem

来源:互联网 发布:java template模板 编辑:程序博客网 时间:2024/06/18 00:15

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549

网络流最大流入门。



#include <iostream>#include <stdio.h>#include <math.h>#include <string.h>#include <queue>#define INF 1e7using namespace std;const int maxn = 20;int cap[maxn][maxn];int flow[maxn][maxn];int N,M,S,D;int maxFlow;int minFlow[maxn];int pre[maxn];void Edmonds_Karp(){    int u,v;    maxFlow = 0;    queue<int>qu;    while(true)    {        memset(minFlow,0,sizeof(minFlow));        minFlow[S] = INF;        qu.push(S);        while(!qu.empty())        {            u = qu.front();            qu.pop();            for(v = 1; v <= N; v++)            {                if(minFlow[v]==0 && flow[u][v] < cap[u][v])                {                    pre[v] = u;                    qu.push(v);                    minFlow[v] = min(minFlow[u],cap[u][v]-flow[u][v]);                }            }        }        if(minFlow[D] == 0)  ///没有路径            break;        maxFlow += minFlow[D];        for(v = D; v != S; v = pre[v])        {            u = pre[v];            flow[u][v] += minFlow[D];            flow[v][u] -= minFlow[D];        }    }}int main(){    int T;    int x,y,c,Case=0;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&N,&M);        memset(cap,0,sizeof(cap));        memset(flow,0,sizeof(flow));        for(int i = 1; i <= M; i++)        {            scanf("%d%d%d",&x,&y,&c);            cap[x][y] += c;        }        S = 1;        D = N;        Edmonds_Karp();        printf("Case %d: %d\n",++Case,maxFlow);    }    return 0;}


原创粉丝点击