zoukankan      html  css  js  c++  java
  • HDU 3549 Flow Problem

    Flow Problem

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


    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
     
    Source
     //有一网络流入门模板题目,给下届的小朋友又一敲代码练习的机会
    Recommend
    zhengfeng

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <queue>
    #define N 1003
    using namespace std;
    int flow[N][N],cap[N][N];
    int f[N];
    int n,m;
    int BFS()
    {
        int u,v;
        int a[N];
        int sc=0;
        for(int i=1;i<=n;i++)//把里面的这种初始化过程改成memset,那么将变成4900+Ms,而这样就60+Ms
              for(int j=1;j<=n;j++)//相差好大呀
                 flow[i][j]=0;
        for(;;)
        {
            queue<int>Q;
            for(int i=1;i<=n;i++)
               a[i]=0;
            a[1]=N;
            Q.push(1);
            while(!Q.empty())
            {
                u=Q.front();Q.pop();
                 for(v=1;v<=n;v++)
                   if(!a[v]&&cap[u][v]-flow[u][v]>0)
                  {
                      f[v]=u;
                      Q.push(v);
                      a[v]=a[u]<cap[u][v]-flow[u][v]?a[u]:cap[u][v]-flow[u][v];
                  }

            }
            if(a[n]==0) break;
            for(u=n;u!=1;u=f[u])
            {
                flow[f[u]][u]+=a[n];
                flow[u][f[u]]-=a[n];
            }
            sc+=a[n];
        }
        return sc;
    }
    int main()
    {
        int t,T=1;
        int u,v,c;
        scanf("%d",&t);
        while(t--)
        {
           scanf("%d%d",&n,&m);
           for(int i=1;i<=n;i++)
              for(int j=1;j<=n;j++)
                 cap[i][j]=0;
           while(m--)
           {
               scanf("%d%d%d",&u,&v,&c);
               cap[u][v]+=c;
           }
           printf("Case %d: %d\n",T++,BFS());
        }
        return 0;
    }

  • 相关阅读:
    Python·安装扩展包的几种方法
    Arduino系列硬件资源介绍
    树莓派USB摄像头的使用
    树莓派frp服务器和客户端配置教程
    树莓派frp内网穿透
    用Windows远程桌面连接树莓派的方法
    控制窗体的位置和大小
    树霉派更换软件镜像源
    I2C的库函数应用示例
    I2C总线的Arduino库函数
  • 原文地址:https://www.cnblogs.com/372465774y/p/2592753.html
Copyright © 2011-2022 走看看