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): 15980    Accepted Submission(s): 7594


    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   |   We have carefully selected several similar problems for you:  1533 3338 1569 3572 3416 
    正式进军省选,首先攻克网络流大关,此题是网络流之最大流问题
    http://wenku.baidu.com/link?url=VTl4q3Yy3D58pfJ7IRxrlWI5Y09FSdCt6uZ8uIHUlPH2csq5WuHbBX5kPXmyFhkqT-FXxGeNYl2nEa4W1ZpLyMvpYoaE4Of-DNyfXYQ3MU_
     1 #include "bits/stdc++.h"
     2 #define mem(a,b) memset(a,b,sizeof(a))
     3 using namespace std;
     4 typedef long long LL;
     5 const int MAX=205;
     6 const int oo=233333333;
     7 int n,m;
     8 int g[MAX][MAX],layer[MAX];
     9 bool vis[MAX];
    10 bool countlayer(){
    11     int i,j,u,v;
    12     queue <int> q;
    13     mem(layer,-1);layer[1]=0,q.push(1);
    14     while (!q.empty()){
    15         u=q.front();q.pop();
    16         for (i=1;i<=n;i++){
    17             if (g[u][i]>0 && layer[i]==-1){
    18                 layer[i]=layer[u]+1;
    19                 if (i==n)
    20                     return true;
    21                 else
    22                     q.push(i);
    23             }
    24         }
    25     }
    26     return false;
    27 }
    28 int dinic(){
    29     int i,j,ans(0),vs,ve;
    30     int nmc,nmc_v;
    31     deque <int> q;
    32     while (countlayer()){
    33         q.push_back(1);
    34         mem(vis,0),vis[1]=1;
    35         while (!q.empty()){
    36             int nd=q.back();
    37             if (nd==n){
    38                 nmc=oo;
    39                 for (i=1;i<q.size();i++){
    40                     vs=q[i-1],ve=q[i];
    41                     if (g[vs][ve]>0){
    42                         if (nmc>g[vs][ve]){
    43                             nmc=g[vs][ve];
    44                             nmc_v=vs;
    45                         }
    46                     }
    47                 }
    48                 ans+=nmc;
    49                 for (i=1;i<q.size();i++){
    50                     vs=q[i-1],ve=q[i];
    51                     g[vs][ve]-=nmc;
    52                     g[ve][vs]+=nmc;
    53                 }
    54                 while (!q.empty() && q.back()!=nmc_v){
    55                     vis[q.back()]=0;
    56                     q.pop_back();
    57                 }
    58             }
    59             else{
    60                 for (i=1;i<=n;i++){
    61                     if (g[nd][i]>0 && layer[i]==layer[nd]+1 && !vis[i]){
    62                         q.push_back(i);
    63                         vis[i]=1;
    64                         break;
    65                     }
    66                 }
    67                 if (i==n+1)
    68                     q.pop_back();
    69             }
    70         }
    71     }
    72     return ans;
    73 }
    74 int main(){
    75     freopen ("dinic.in","r",stdin);
    76     freopen ("dinic.out","w",stdout);
    77     int i,j;
    78     int u,v,w;
    79     while (~scanf("%d%d",&m,&n)){
    80         mem(g,0);
    81         for (i=1;i<=m;i++){
    82             scanf("%d%d%d",&u,&v,&w);
    83             g[u][v]+=w;
    84         }
    85         printf("%d
    ",dinic());
    86     }
    87     return 0;
    88 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    封装/继承
    模板
    常用模块-re模块1
    包常用模块
    模块和软件开发的目录规范
    Hadoop 综合大作业
    hive基本操作与应用
    用mapreduce 处理气象数据集
    熟悉常用的HBase操作,编写MapReduce作业
    爬虫大作业
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/6088892.html
Copyright © 2011-2022 走看看