• Drainage Ditches


    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
    ***********************************************************************************************************
    基本dinic算法。先bfs()求到源点的最短距离,再find()每次余下残留网络。
    ***********************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<cstdio>
     7 #include<queue>
     8 #include<vector>
     9 #include<stack>
    10 using namespace std;
    11 const  int inf=1<<28;
    12 int  dis[10001],i,j;
    13 int map[1001][1001];
    14 int n,m;
    15 int source,sink;
    16 queue<int>Q;
    17 int min(int a,int b)
    18  {
    19      return a>b?b:a;
    20  }
    21 int bfs()//找最短路径(距源点的和sap算法正好相反)
    22  {
    23     if(!Q.empty())Q.pop();
    24     memset(dis,-1,sizeof(dis));
    25     Q.push(source);
    26     dis[source]=0;
    27     while(!Q.empty())
    28      {
    29          int h=Q.front();
    30          Q.pop();
    31          for(int it=1;it<=n;it++)
    32           if(dis[it]<0&&map[h][it]>0)
    33           {
    34              dis[it]=dis[h]+1;
    35              Q.push(it);
    36           }
    37 
    38      }
    39      if(dis[sink]>0)//满足到达源点返回真,否则返回假
    40       return 1;
    41     else
    42      return 0;
    43  }
    44  int find(int x,int low)//找到一条路径,减cap最小的路径,留下残留网络
    45   {
    46       int g,f=0;
    47       if(x==sink)
    48        return low;
    49       for(g=1;g<=n;g++)
    50        if(map[x][g]>0&&(dis[g]==dis[x]+1)&&(f=find(g,min(low,map[x][g]))))//找瓶颈值
    51         {
    52             map[x][g]-=f;
    53             map[g][x]+=f;
    54             return f;
    55         }
    56        return 0;
    57   }
    58   int main()
    59   {
    60       while(scanf("%d%d",&m,&n)!=EOF)
    61       {
    62           int st,en,c;
    63           memset(map,0,sizeof(map));
    64           for(i=1;i<=m;i++)
    65           {
    66            cin>>st>>en>>c;
    67            map[st][en]+=c;//防止两点之间有多条路
    68           }
    69           int ans=0;
    70           int tans;
    71           source=1;sink=n;
    72           while(bfs())
    73           {
    74               while(tans=find(source,inf))//一次bfs()多次寻找直到找不到为止
    75                 ans+=tans;
    76           }
    77         cout<<ans<<endl;
    78       }
    79       return 0;
    80   }
    View Code

    坚持!!!!!

  • 相关阅读:
    redis之windows安装
    ubuntu安装 ssh
    less乱码ESC显示问题
    linux 命令/目录 名称 英文单词 缩写 助记
    设计模式-策略模式
    设计模式-责任链模式
    ApplicationContexAware的作用
    ApplicationEvent事件机制
    mybatis注解版延迟加载、立即加载、一级缓存、二级缓存
    mybatis xml版延迟加载、立即加载、一级缓存、二级缓存
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3276842.html
Copyright © 2020-2023  润新知