zoukankan      html  css  js  c++  java
  • HDU 1532.Drainage Ditches-网络流最大流

    Drainage Ditches

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 22789    Accepted Submission(s): 10905


    Problem Description
    Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
    Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
    Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 
     
    Input
    The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
     
    Output
    For each case, output a single integer, the maximum rate at which water may emptied from the pond. 
     
    Sample Input
    5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
     
    Sample Output
    50
     
    Source
     
     
     
     
    代码:
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int INF=0x3f3f3f3f;
     4 const int N=200+10;
     5 int flow[N][N];
     6 int mark[N],pre[N];
     7 int n,m,f;
     8 void max_flow(){
     9     while(1){
    10         memset(mark,0,sizeof(mark));
    11         memset(pre,0,sizeof(pre));
    12         queue<int>Q;
    13         mark[1]=1;
    14         Q.push(1);
    15         while(!Q.empty()){
    16             int cnt=Q.front();
    17             Q.pop();
    18             if(cnt==n)break;
    19             for(int i=1;i<=n;i++){
    20                 if(!mark[i]&&flow[cnt][i]>0){
    21                     mark[i]=1;
    22                     Q.push(i);
    23                     pre[i]=cnt;
    24                 }
    25             }
    26         }if(!mark[n])break;
    27         int minx=INF;
    28         for(int i=n;i!=1;i=pre[i]){
    29             minx=min(flow[pre[i]][i],minx);
    30         }
    31         for(int i=n;i!=1;i=pre[i]){
    32             flow[pre[i]][i]-=minx;
    33             flow[i][pre[i]]+=minx;
    34         }
    35         f+=minx;
    36     }
    37 }
    38 int main(){
    39     while(~scanf("%d%d",&m,&n)){
    40         memset(flow,0,sizeof(flow));
    41         for(int i=0;i<m;i++){
    42             int u,v,len;
    43             scanf("%d%d%d",&u,&v,&len);
    44             flow[u][v]+=len;
    45         }
    46         f=0;
    47         max_flow();
    48         printf("%d
    ",f);
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    [leetcode]133. Clone Graph 克隆图
    [leetcode]366. Find Leaves of Binary Tree捡树叶
    [leetcode]311. Sparse Matrix Multiplication 稀疏矩阵相乘
    [leetcode]151. Reverse Words in a String翻转给定字符串中的单词
    [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
    Union and Intersection of two sorted lists 并集和交集
    [leetcode]205. Isomorphic Strings 同构字符串
    [leetcode]428. Serialize and Deserialize N-ary Tree序列化与反序列化N叉树
    [leetcode]364. Nested List Weight Sum II嵌套列表加权和II
    属性 元素的内容 创建,插入和删除节点 虚拟节点
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9740904.html
Copyright © 2011-2022 走看看