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): 12860    Accepted Submission(s): 6128


    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   |   We have carefully selected several similar problems for you:  3572 3416 3081 3491 1533 
     
    同hdu 1532,也是最大流模板题,不过先输入水沟的顶点数,再输入水沟数。
     
    题意:就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速,本题就是让你求出最大流速,无疑要运用到求最大流了。题中N为水沟的顶点数,M为水沟数,接下来Si,Ei,Ci分别是水沟的起点,终点以及其容量。求源点1到终点M的最大流速。注意重边
     
    附上代码:
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #define INF 1e9
    #define xmin(a,b) a<b?a:b
    #define N 1005
    #define CL(a,b) memset(a,b,sizeof(a))
    using namespace std;
    int n,m;
    int mat[N][N];
    int pre[N];
    bool vis[N];
    
    bool BFS()
    {
        int cur;
        queue <int> q;
        CL(pre,0);
        CL(vis,false);
        vis[1]=true;
        q.push(1);
        while(!q.empty())
        {
            cur=q.front();
            q.pop();
            if(cur==n) return true;
            for(int i=1; i<=n; i++)
            {
                if(!vis[i] && mat[cur][i])
                {
                    vis[i]=true;
                    pre[i]=cur;
                    q.push(i);
                }
            }
        }
        return false;
    }
    
    int max_flow()
    {
        int ans=0;
        while(1)
        {
            if(!BFS()) return ans;
            int Min=INF;
            for(int i=n; i!=1; i=pre[i])
                Min=xmin(Min,mat[pre[i]][i]);
            for(int i=n; i!=1; i=pre[i])
            {
                mat[pre[i]][i]-=Min;
                mat[i][pre[i]]+=Min;
            }
            ans+=Min;
        }
    }
    
    int main()
    {
        int i,j,T,Case;
        scanf("%d",&T);
        for(Case=1;Case<=T;Case++)
        {
            scanf("%d%d",&n,&m);
            CL(mat,0);
            int a,b,c;
            while(m--)
            {
                scanf("%d%d%d",&a,&b,&c);
                mat[a][b]+=c;
            }
            printf("Case %d: ",Case);
            printf("%d
    ",max_flow());
        }
    
        return 0;
    }
  • 相关阅读:
    “贴身外教”是英语口语的学习方法综合解决方案
    浅谈提高英语口语的最有效方法
    【PERL】Perl默认的内部变量
    Perl——哈希的创建和引用
    linux和windows下,C/C++开发的延时函数,sleep函数
    linux标准库#include <unistd.h>与windows的#include <windows.h>(C语言开发)
    linux 如何显示一个文件的某几行(中间几行)
    使程序在Linux下后台运行
    Perl中关于数组的输出——需要注意的地方
    linux下C/C++,多线程pthread
  • 原文地址:https://www.cnblogs.com/pshw/p/5681177.html
Copyright © 2011-2022 走看看