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

  • 相关阅读:
    UVA 11149.Power of Matrix-矩阵快速幂倍增
    51nod 1137.矩阵乘法-矩阵乘法
    HDU 4920.Matrix multiplication-矩阵乘法
    HDU 6237.A Simple Stone Game-欧拉函数找素因子 (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
    HDU 6235.Permutation (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
    POJ 2226.Muddy Fields-二分图最大匹配(最小点覆盖)
    POJ 3041.Asteroids-Hungary(匈牙利算法)
    HDU 2063.过山车-Hungary(匈牙利算法)
    Codeforces 832 B. Petya and Exam-字符串匹配
    HDU 6038.Function-数学+思维 (2017 Multi-University Training Contest
  • 原文地址:https://www.cnblogs.com/372465774y/p/2592753.html
Copyright © 2011-2022 走看看