• 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 }
  • 相关阅读:
    PHP 反射 ReflectionClass
    go-字符串生成数据库字段
    grpc类型
    ubuntu安装zookeeper集群
    ubuntu安装JDK
    zookeeper分布式读写锁
    golang利用gob序列化struct对象保存到本地(转载)
    golang手动管理内存(转载)
    类json格式字符串打印
    研二寒假---关于Qt&CV曲线算法问题
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9740904.html
Copyright © 2020-2023  润新知