hdu 3549 Flow Problem(最大流模版题 Edmonds-Karp算法)

来源:互联网 发布:梅林传奇 滚娘 知乎 编辑:程序博客网 时间:2024/05/19 04:53

Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 5984    Accepted Submission(s): 2786


Problem Description
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
23 21 2 12 3 13 31 2 12 3 11 3 1
 

Sample Output
Case 1: 1Case 2: 2
最大流模版题,Edmonds-Karp算法
while(存在增广路)
{
        找到任意一条增广路;
        更新残余网络;
}
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdlib>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define ll long long#define eps 1e-6using namespace std;const int INF=1000000000;int c[20][20],f[20][20];int n,m;int bfs(){    queue<int>Q;    int a[20],pre[20];    int ans=0;    memset(f,0,sizeof(f));    while(1)    {        memset(a,0,sizeof(a));        a[1]=INF;        Q.push(1);        while(!Q.empty())        {            int u=Q.front();            Q.pop();            for(int v=1; v<=n; v++)                if(!a[v]&&c[u][v]>f[u][v])                {                    pre[v]=u;                    Q.push(v);                    a[v]=min(a[u],c[u][v]-f[u][v]);                }        }        if(!a[n]) break;        for(int u=n;u!=1;u=pre[u])        {            f[pre[u]][u]+=a[n];            f[u][pre[u]]-=a[n];        }        ans+=a[n];    }    return ans;}int main(){    int t,ca=0;    int x,y,z;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        memset(c,0,sizeof(c));        while(m--)        {            scanf("%d%d%d",&x,&y,&z);            c[x][y]+=z;        }        printf("Case %d: %d\n",++ca,bfs());    }    return 0;}


原创粉丝点击