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模板
  • 相关阅读:
    axios
    JavaScript ES6部分语法
    JSP 基础之 JSTL <c:forEach>用法
    JS ajxa请求 返回数据
    java中的各种数据类型在内存中存储的方式
    sql之left join、right join、inner join的区别
    错误”ORA-12560: TNS: 协议适配器错误“解决方法
    Hibernate 和 Mybatis的区别
    eclipse启动几秒后报错 (一闪而过)
    sql ---- count 误区
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4318160.html
Copyright © 2011-2022 走看看