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

    Drainage Ditches
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 59210   Accepted: 22737

    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

    会有重边出现,要小心。
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 #include<algorithm>
     5 using namespace std;
     6 const int M = 210 , inf = 0x3f3f3f3f ;
     7 int m , n ;
     8 int map[M][M] ;
     9 int dis[M];
    10 
    11 bool bfs ()
    12 {
    13     queue <int> q ;
    14     while (!q.empty ())
    15         q.pop () ;
    16     memset (dis , 0xff , sizeof(dis)) ;
    17     dis[1] = 0 ;
    18     q.push (1) ;
    19     while (!q.empty () ) {
    20         int u = q.front () ;
    21         q.pop () ;
    22         for (int v = 1 ; v <= n ; v++) {
    23             if (map[u][v] && dis[v] == -1) {
    24                 dis[v] = dis[u] + 1 ;
    25                 q.push (v) ;
    26             }
    27         }
    28     }
    29     if (dis[n] > 0)
    30         return true ;
    31     return false ;
    32 }
    33 
    34 int find (int u , int low)
    35 {
    36     int a = 0 ;
    37     if (u == n)
    38         return low ;
    39     for (int v = 1 ; v <= n ; v++) {
    40         if (map[u][v] && dis[v] == dis[u] + 1 && (a = find (v , min(map[u][v] , low)))) {
    41             map[u][v] -= a ;
    42             map[v][u] += a ;
    43             return a ;
    44         }
    45     }
    46     return 0 ;
    47 }
    48 
    49 int main ()
    50 {
    51  //   freopen ("a.txt" , "r" , stdin) ;
    52     int u , v , w ;
    53     int ans ;
    54     while (~ scanf ("%d%d" , &m , &n)) {
    55         memset (map , 0 , sizeof(map)) ;
    56       //  printf ("m = %d , n = %d
    " , m , n) ;
    57         while (m--) {
    58             scanf ("%d%d%d" , &u , &v , &w) ;
    59             map[u][v] += w ;
    60         }
    61         int maxn = 0 ;
    62         while (bfs()) {
    63             if (ans = find(1 , inf))
    64                 maxn += ans ;
    65         }
    66         printf ("%d
    " , maxn) ;
    67     }
    68     return 0 ;
    69 }
    标准的dinic模板
  • 相关阅读:
    《挑战程序设计竞赛》 读后感
    基于SOAP的xml网络交互心得
    不用客户端,轻松下视频
    在cmd窗口中查询android的sqlite3数据库表之步骤
    单链表的插入删除以及逆转
    java中排序一个字符串数组
    求质因数
    指针与引用的区别
    统计查询-sql
    ---随心买统计查询
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4318160.html
Copyright © 2011-2022 走看看