zoukankan      html  css  js  c++  java
  • Drainage Ditches(dinic模板)

    描述

    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.

    输入

    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.

    输出

    For each case, output a single integer, the maximum rate at which water may emptied from the pond.

    样例输入

    5 4
    1 2 40
    1 4 20
    2 4 20
    2 3 30
    3 4 10

    样例输出

     50

    最大流问题,dinic算法模板

    关于理论:http://www.cnblogs.com/SYCstudio/p/7260613.html

    代码实现:https://www.cnblogs.com/qijinbiao/archive/2012/07/18/2598197.html

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <queue>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int N=205;
     8 const int MAX=0x3f3f3f3f;
     9 struct Edge
    10 {
    11     int u,v,c;
    12     int next;
    13 }edge[2*N];
    14 int n,m;
    15 int edn;//边数
    16 int pre[N];//父亲
    17 int dep[N];//层数
    18 int sp,tp;//源点,汇点
    19 queue<int> q;
    20 
    21 void init()
    22 {
    23     edn=0;
    24     memset(pre,-1,sizeof(pre));
    25     sp=1; tp=n;
    26 }
    27 
    28 void addedge(int u,int v,int c)//加边
    29 {
    30     edge[edn].u=u; edge[edn].v=v; edge[edn].c=c;
    31     edge[edn].next=pre[u]; pre[u]=edn++;
    32 
    33     edge[edn].u=v; edge[edn].v=u; edge[edn].c=0;
    34     edge[edn].next=pre[v]; pre[v]=edn++;
    35 }
    36 
    37 int bfs()//建分层图
    38 {
    39     memset(dep,-1,sizeof(dep));
    40     dep[sp]=0;
    41     q.push(sp);
    42     while(!q.empty()){
    43         int cur=q.front();
    44         q.pop();
    45         for(int i=pre[cur];i!=-1;i=edge[i].next){
    46             int k=edge[i].v;
    47             if(dep[k]==-1&&edge[i].c>0){
    48                 dep[k]=dep[cur]+1;
    49                 q.push(k);
    50             }
    51         }
    52     }
    53     return dep[tp]!=-1;
    54 }
    55 
    56 int dfs(int a,int b)//寻找增广路
    57 {
    58     int r=0;
    59     if(a==tp)return b;
    60     for(int i=pre[a];i!=-1&&r<b;i=edge[i].next){
    61         int u=edge[i].v;
    62         if(edge[i].c>0&&dep[u]==dep[a]+1){
    63             int x=min(edge[i].c,b-r);
    64             x=dfs(u,x);
    65             r+=x;
    66             edge[i].c-=x;
    67             edge[i^1].c+=x;
    68         }
    69     }
    70     if(!r) dep[a]=-2;
    71     return r;
    72 }
    73 
    74 int dinic()//bfs+dfs
    75 {
    76     int res=0,t;
    77     while(bfs()){
    78         while(t=dfs(sp,MAX))
    79             res+=t;
    80     }
    81     return res;
    82 }
    83 
    84 int main()
    85 {
    86     int u,v,c;
    87     while(~scanf("%d%d",&m,&n)){
    88         init();
    89         for(int i=0;i<m;i++){
    90             scanf("%d%d%d",&u,&v,&c);
    91             addedge(u,v,c);
    92         }
    93         printf("%d
    ",dinic());
    94     }
    95     return 0;
    96 }
  • 相关阅读:
    OsharpNS轻量级.net core快速开发框架简明入门教程-基于Osharp实现自己的业务功能
    OsharpNS轻量级.net core快速开发框架简明入门教程-代码生成器的使用
    一起创业吧:兼职程序员接单平台
    .NET、PHP、MySql、JS中的时间戳你每次是手写还是复制?这篇文章让你一次性搞懂
    .NET和PHP程序员如何通过技术快速变现
    .NET程序员我是如何通过一个产品在2年内买车买房
    .NET方法无限传参数技术
    .NET 增加扩展方法
    毕业10年总结与2019展望
    jQuery 1.9/2.0/2.1及其以上 on 无效的解决办法
  • 原文地址:https://www.cnblogs.com/ChangeG1824/p/9530457.html
Copyright © 2011-2022 走看看