zoukankan      html  css  js  c++  java
  • HDU3549_Flow Problem(网络流/EK)

    Flow Problem

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


    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
    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
     

    Author
    HyperHexagon

    解题报告

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #define N 30
    #define inf 99999999
    using namespace std;
    int n,m,a[N],flow,pre[N];
    int Edge[N][N];
    queue<int >Q;
    int bfs()
    {
        while(!Q.empty())
        Q.pop();
        memset(a,0,sizeof(a));
        memset(pre,0,sizeof(pre));
        Q.push(1);
        pre[1]=1;
        a[1]=inf;
        while(!Q.empty())
        {
            int u=Q.front();
            Q.pop();
            for(int v=1;v<=n;v++)
            {
                if(!a[v]&&Edge[u][v]>0)
                {
                    pre[v]=u;
                    a[v]=min(Edge[u][v],a[u]);
                    Q.push(v);
                }
            }
            if(a[n])break;
        }
        if(!a[n])
        return -1;
        else return a[n];
    }
    void ek()
    {
        int a,i;
        while((a=bfs())!=-1)
        {
            for(i=n;i!=1;i=pre[i])
            {
                Edge[pre[i]][i]-=a;
                Edge[i][pre[i]]+=a;
            }
            flow+=a;
        }
    }
    int main()
    {
        int t,i,j,u,v,w,k=1;
        scanf("%d",&t);
        while(t--)
        {
            flow=0;
            memset(Edge,0,sizeof(Edge));
            scanf("%d%d",&n,&m);
            for(i=0;i<m;i++)
            {
                scanf("%d%d%d",&u,&v,&w);
                Edge[u][v]+=w;
            }
            ek();
            printf("Case %d: ",k++);
            printf("%d
    ",flow);
        }
        return 0;
    }
    


  • 相关阅读:
    #3.14 Piday#我的圆周率日
    FUI- 我离钢铁侠还差几步?
    POJ 3617 Best Cow Line (贪心)
    POJ 2386 Lake Counting (水题,DFS)
    POJ 1852 Ants (等价思考)
    CCF 201403-3 命令行选项 (STL模拟)
    CCF 201403-2 窗口 (STL模拟)
    CCF 201403-1 相反数 (水题)
    CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
    CCF 201312-3 最大的矩形 (暴力,离散化)
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7263862.html
Copyright © 2011-2022 走看看