zoukankan      html  css  js  c++  java
  • HDU 1532 Drainage Ditches

    Drainage Ditches



    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
     
     
     1 #include<cstdio>
     2 #include<vector>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 using namespace std;
     7 
     8 struct edge
     9 {
    10     int from,to,cap,flow;
    11     edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
    12 };
    13 
    14 const int inf=999999;
    15 int m,n;
    16 int a[205];
    17 int p[205];
    18 vector<int>G[405];
    19 vector<edge>edges;
    20 
    21 void init()
    22 {
    23     for(int i=0;i<n;i++)
    24     G[i].clear();
    25     edges.clear();
    26 }
    27 
    28 void addedge(int from,int to,int cap)
    29 {
    30     edges.push_back(edge(from,to,cap,0));
    31     edges.push_back(edge(to,from,0,0));
    32     int m=edges.size();
    33     G[to].push_back(m-1);
    34     G[from].push_back(m-2);
    35 }
    36 
    37 int maxflow(int s,int t)
    38 {
    39     int flow=0;
    40     while(1)
    41     {
    42         memset(a,0,sizeof(a));
    43         queue<int>q;
    44         q.push(s);
    45         a[s]=inf;
    46         while(!q.empty())
    47         {
    48             int x=q.front();
    49             q.pop();
    50             for(int i=0;i<G[x].size();i++)
    51             {
    52                 edge& e=edges[G[x][i]];
    53                 if(!a[e.to]&&e.cap>e.flow)
    54                 {
    55                     p[e.to]=G[x][i];
    56                     a[e.to]=min(a[x],e.cap-e.flow);
    57                     q.push(e.to);
    58                 }
    59             }
    60             if(a[t])break;
    61         }
    62         if(!a[t])break;
    63         for(int u=t;u!=s;u=edges[p[u]].from)
    64         {
    65             edges[p[u]].flow+=a[t];
    66             edges[p[u]^1].flow-=a[t];
    67         }
    68         flow+=a[t];
    69     }
    70     return flow;
    71 }
    72 
    73 int main()
    74 {
    75     int a,b,c;
    76     while(scanf("%d%d",&m,&n)!=EOF)
    77     {
    78         init();
    79         for(int i=0;i<m;i++)
    80         {
    81             scanf("%d%d%d",&a,&b,&c);
    82             addedge(a,b,c);
    83         }
    84         int ans=maxflow(1,n);
    85         printf("%d
    ",ans);
    86     }
    87     return 0;
    88 }
  • 相关阅读:
    数组中的逆序对
    第一个只出现一次的字符
    丑数
    把数组排成最小的数
    整数中出现1的个数
    连续子数组最大和
    JS之window对象
    JS之递归(例题:猴子吃桃)
    JS中函数的基础知识
    JS数组2(冒泡排列、数组里面查找数据)
  • 原文地址:https://www.cnblogs.com/homura/p/4721031.html
Copyright © 2011-2022 走看看