zoukankan      html  css  js  c++  java
  • POJ 1273 Drainage Ditches

    Drainage Ditches
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 52160   Accepted: 19830

    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

    最基本的网络流问题,今天学习了一下网络流,就先写了这道,直接套Ford-Fulkerson算法模板即可

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<vector>
     4 #include<cstring>
     5 #include<algorithm>
     6 #define INF 0x7fffffff
     7 #define MAX_V 210
     8 
     9 using namespace std;
    10 
    11 struct edge
    12 {
    13     int to;
    14     int cap;
    15     int rev;
    16 };
    17 
    18 int m,n;
    19 bool used[MAX_V];
    20 vector<edge> G[MAX_V];
    21 
    22 void add_edge(int _from,int _to,int _cap)
    23 {
    24     edge temp;
    25     temp.to=_to;
    26     temp.cap=_cap;
    27     temp.rev=G[_to].size();
    28     G[_from].push_back(temp);
    29     temp.to=_from;
    30     temp.cap=0;
    31     temp.rev=G[_from].size()-1;
    32     G[_to].push_back(temp);
    33 }
    34 
    35 int dfs(int v,int t,int f)
    36 {
    37     if(v==t)
    38         return f;
    39 
    40     used[v]=true;
    41     for(int i=0;i<G[v].size();i++)
    42     {
    43         edge &e=G[v][i];
    44         if(!used[e.to]&&e.cap>0)
    45         {
    46             int d=dfs(e.to,t,min(f,e.cap));
    47             if(d>0)
    48             {
    49                 e.cap-=d;
    50                 G[e.to][e.rev].cap+=d;
    51                 return d;
    52             }
    53         }
    54     }
    55 
    56     return 0;
    57 }
    58 
    59 int max_flow(int s,int t)
    60 {
    61     int flow=0;
    62     while(true)
    63     {
    64         memset(used,false,sizeof(used));
    65         int f=dfs(s,t,INF);
    66         if(f==0)
    67             return flow;
    68         flow+=f;
    69     }
    70 }
    71 
    72 int main()
    73 {
    74     while(scanf("%d %d",&n,&m)==2)
    75     {
    76         for(int i=1;i<=m;i++)
    77             G[i].clear();
    78 
    79         int s,e,c;
    80         for(int i=1;i<=n;i++)
    81         {
    82             scanf("%d %d %d",&s,&e,&c);
    83             add_edge(s,e,c);
    84         }
    85         printf("%d
    ",max_flow(1,m));
    86     }
    87 
    88     return 0;
    89 }
    [C++]

    下面是用Dinic算法模板写的,与上面的相比速度要快一点

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<vector>
      4 #include<cstring>
      5 #include<algorithm>
      6 #include<queue>
      7 #define INF 0x7fffffff
      8 #define MAX_V 210
      9 
     10 using namespace std;
     11 
     12 struct edge
     13 {
     14     int to;
     15     int cap;
     16     int rev;
     17 };
     18 
     19 int m,n;
     20 int iter[MAX_V];
     21 int level[MAX_V];
     22 vector<edge> G[MAX_V];
     23 
     24 void add_edge(int _from,int _to,int _cap)
     25 {
     26     edge temp;
     27     temp.to=_to;
     28     temp.cap=_cap;
     29     temp.rev=G[_to].size();
     30     G[_from].push_back(temp);
     31     temp.to=_from;
     32     temp.cap=0;
     33     temp.rev=G[_from].size()-1;
     34     G[_to].push_back(temp);
     35 }
     36 
     37 void bfs(int s)
     38 {
     39     memset(level,-1,sizeof(level));
     40     queue<int> que;
     41     level[s]=0;
     42     que.push(s);
     43     while(!que.empty())
     44     {
     45         int v=que.front();
     46         que.pop();
     47         for(int i=0;i<G[v].size();i++)
     48         {
     49             edge &e=G[v][i];
     50             if(e.cap>0&&level[e.to]<0)
     51             {
     52                 level[e.to]=level[v]+1;
     53                 que.push(e.to);
     54             }
     55         }
     56     }
     57 }
     58 
     59 int dfs(int v,int t,int f)
     60 {
     61     if(v==t)
     62         return f;
     63 
     64     for(int &i=iter[v];i<G[v].size();i++)
     65     {
     66         edge &e=G[v][i];
     67         if(e.cap>0&&level[v]<level[e.to])
     68         {
     69             int d=dfs(e.to,t,min(f,e.cap));
     70             if(d>0)
     71             {
     72                 e.cap-=d;
     73                 G[e.to][e.rev].cap+=d;
     74                 return d;
     75             }
     76         }
     77     }
     78 
     79     return 0;
     80 }
     81 
     82 
     83 int max_flow(int s,int t)
     84 {
     85     int flow=0;
     86 
     87     while(true)
     88     {
     89         bfs(s);
     90         if(level[t]<0)
     91             return flow;
     92         memset(iter,0,sizeof(iter));
     93         int f;
     94         while((f=dfs(s,t,INF))>0)
     95             flow+=f;
     96     }
     97 }
     98 
     99 
    100 int main()
    101 {
    102     while(scanf("%d %d",&n,&m)==2)
    103     {
    104         for(int i=1;i<=m;i++)
    105             G[i].clear();
    106 
    107         int s,e,c;
    108         for(int i=1;i<=n;i++)
    109         {
    110             scanf("%d %d %d",&s,&e,&c);
    111             add_edge(s,e,c);
    112         }
    113         printf("%d
    ",max_flow(1,m));
    114     }
    115 
    116     return 0;
    117 }
    [C++]
  • 相关阅读:
    Linux中常用操作命令
    JQuery Each循环遍历每个元素
    get set
    Launch Screen在iOS7/8中的实现
    程序猿必备的Git教程
    浏览器的工作原理:新式网络浏览器幕后揭秘
    浏览器的工作原理:新式网络浏览器幕后揭秘
    游览器中javascript的执行过程
    游览器中javascript的执行过程
    浅析 Cordova for iOS
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3560136.html
Copyright © 2011-2022 走看看