zoukankan      html  css  js  c++  java
  • [POJ 1273] Drainage Ditches & 最大流Dinic模板

    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

    Source

     
    【题目大意】
    就是最普通的网络流问题,什么条件都没变。
    【题解】
    终于会dinic了TAT啊,这是我A的第一道网络流题啊
    要纪念啊=-=
    纯dinic模板,我用邻接矩阵,然而什么时候要学一学边集数组版本=-= 908K 0ms TAT终于过了!
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<stdlib.h>
     5 #include<string>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue> 
     9 using namespace std;
    10 int c[251];              // 分层图
    11 int a[251][251];          //邻接矩阵 
    12 int n,m,ans;
    13 inline int min(int a,int b) {return a<b?a:b;}
    14 int bfs() {
    15     memset(c,0xff,sizeof(c));
    16     c[1]=0;
    17     queue<int> q;
    18     q.push(1);
    19     while(!q.empty()) {
    20         int top=q.front();
    21         q.pop();
    22         for (int i=1;i<=n;++i) {
    23             if(c[i]<0&&a[top][i]>0) {
    24                 c[i]=c[top]+1;
    25                 q.push(i);
    26             }
    27         }
    28     }
    29     if(c[n]>0) return 1;
    30     else return 0;
    31 }
    32 int dfs(int x,int low) {
    33     if(x==n) return low;
    34     for (int i=1,k;i<=n;++i)
    35         if(a[x][i]>0&&c[i]==c[x]+1&&(k=dfs(i,min(low,a[x][i])))) {
    36             a[x][i]-=k;
    37             a[i][x]+=k;
    38             return k;
    39         }
    40     return 0;
    41 }
    42 int main() {
    43     while(~scanf("%d%d",&m,&n)) {
    44         memset(a,0,sizeof(a));
    45         for (int i=1;i<=m;++i) {
    46             int aa,bb,cc;
    47             scanf("%d%d%d",&aa,&bb,&cc);
    48             a[aa][bb]+=cc;
    49         }
    50         ans=0;
    51         int t;
    52         while(bfs()) {
    53             while(t=dfs(1,0x7fffff)) ans+=t;
    54         }
    55         printf("%d
    ",ans);
    56     }
    57 }
    View Code
  • 相关阅读:
    Scrapy数据持久化
    Mybatis源码与Spring源码中设计模式的应用总结
    Count-Min Sketch 算法
    加解密算法、消息摘要、消息认证技术、数字签名与公钥证书
    智能卡系统设计(一) 断电保护和数据备份
    TCP/IP协议——ARP详解
    DES算法详解
    Python调用外部程序——os.system()和subprocess.call()
    Python实现截图
    Linux笔记:SSH客户端断开配置
  • 原文地址:https://www.cnblogs.com/TonyNeal/p/poj1273.html
Copyright © 2011-2022 走看看