zoukankan      html  css  js  c++  java
  • Drainage Ditches(最大流入门)

    Drainage Ditches

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


    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
     
    Recommend
    lwg
     

    最大流入门题,参考资料:http://www.wutianqi.com/?p=3107

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 using namespace std;
     6 
     7 const int maxn = 201;
     8 int n, m;  //n:路径数,m:顶点数
     9 int r[maxn][maxn]; //剩余容量,初始值为最大容量
    10 bool vis[maxn];  //在搜寻增广路径时记录已访问顶点
    11 int pre[maxn];  //pre[i]记录前驱顶点,以便记录增广路径中的顶点。
    12                 //注意的是不能记录后继顶点,因为在广搜中,顶点的前驱唯一而后继不唯一
    13 
    14 bool BFS(int s, int t)  //s:源点,t:汇点
    15 {
    16     memset(vis, false, sizeof(vis));
    17     memset(pre, -1, sizeof(pre));
    18     queue<int> que;
    19     que.push(s);
    20     while(!que.empty())
    21     {
    22         int v = que.front();
    23         vis[v] = true;
    24         que.pop();
    25         for(int i = 1; i <= m; i++)  //注意顶点由1开始标记
    26         {
    27             if(r[v][i] && !vis[i])
    28             {
    29                 pre[i] = v;
    30                 if(i == t) return true; //已找到增广路径
    31                 que.push(i);
    32             }
    33         }
    34     }
    35     return false;
    36 }
    37 
    38 int EK(int s, int t)  //s:源点,t:汇点
    39 {
    40     int max_flow = 0, d;  //max_flow:整个网络的最大流,d:为增广路径带来的增量
    41     while(BFS(s, t))
    42     {
    43         int i = pre[t], j;  //设置i,j只是为了敲代码方便
    44         d = r[i][t];
    45         for(; i != s; i = j)  //获得增量d
    46         {
    47             j = pre[i];
    48             if(d > r[j][i]) d = r[j][i];
    49         }
    50         for(i = t; i != s; i = j)  //求残流网络
    51         {
    52             j = pre[i];
    53             r[j][i] -= d;
    54             r[i][j] += d;
    55         }
    56         max_flow += d;
    57     }
    58     return max_flow;
    59 }
    60 
    61 int main()
    62 {
    63     while(scanf("%d %d", &n, &m) != EOF)
    64     {
    65         int s, e, c;
    66         memset(r, 0, sizeof(r));
    67         while(n--)
    68         {
    69             scanf("%d %d %d", &s, &e, &c);
    70             r[s][e] += c;  //不直接输入r[s][e],预防有重边!
    71         }
    72         printf("%d\n", EK(1, m));
    73     }
    74     return 0;
    75 }
    Statistic | Submit | Discuss | Note
  • 相关阅读:
    修改input标签输入样式
    CSS3的transform 转换
    前端小知识--区分get和post请求
    JS面向对象--你真的理解闭包了吗?
    px,em,rem的区别
    傻瓜式教程--实现登录页面的验证码以及验证(VUE)
    基于RBAC权限管理的后台管理系统
    在VUE中实现打印
    关于三层架构的好文章
    RabbitMQ常用命令、管理界面
  • 原文地址:https://www.cnblogs.com/cszlg/p/3033275.html
Copyright © 2011-2022 走看看