zoukankan      html  css  js  c++  java
  • POJ1273 Drainage Ditches[EK()]

    Drainage Ditches
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 41645   Accepted: 15583

    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

    code:

     1 #include <iostream>   
     2 #include <iomanip>   
     3 #include <fstream>   
     4 #include <sstream>   
     5 #include <algorithm>   
     6 #include <string>   
     7 #include <set>   
     8 #include <utility>   
     9 #include <queue>   
    10 #include <stack>   
    11 #include <list>   
    12 #include <vector>   
    13 #include <cstdio>   
    14 #include <cstdlib>   
    15 #include <cstring>   
    16 #include <cmath>   
    17 #include <ctime>   
    18 #include <ctype.h> 
    19 using namespace std;
    20 
    21 #define MAXN 500
    22 #define intmax 99999999
    23 
    24 int cap[MAXN][MAXN];
    25 int flow[MAXN][MAXN];
    26 int a[MAXN];
    27 int pre[MAXN];
    28 int n,m;
    29 
    30 
    31 int EK()
    32 {
    33     queue<int>Que;
    34     int f=0;
    35     memset(flow,0,sizeof(flow));
    36     for(;;)
    37     {
    38         memset(a,0,sizeof(a));
    39         a[1]=intmax;
    40         Que.push(1);
    41         while(!Que.empty())
    42         {
    43             int u=Que.front();
    44             Que.pop();
    45             for(int i=1;i<=m;i++)
    46                 if(!a[i]&&cap[u][i]>flow[u][i])
    47                 {
    48                     pre[i]=u;
    49                     Que.push(i);
    50                     a[i]=a[u]>cap[u][i]-flow[u][i]?cap[u][i]-flow[u][i]:a[u];
    51                 }
    52         }
    53         if(a[m]==0)
    54             break;
    55         for(int i=m;i!=1;i=pre[i])
    56         {
    57             flow[pre[i]][i]+=a[m];
    58             flow[i][pre[i]]-=a[m];
    59         }
    60         f+=a[m];
    61     }
    62     return f;
    63 }
    64 
    65 int main()
    66 {
    67     int a,b,temp;
    68     while(~scanf("%d%d",&n,&m))
    69     {
    70         memset(cap,0,sizeof(cap));
    71         for(int i=0;i<n;i++)
    72         {
    73             scanf("%d%d%d",&a,&b,&temp);
    74             cap[a][b]+=temp;
    75         }
    76         printf("%d\n",EK());
    77     }
    78     return 0;
    79 }
  • 相关阅读:
    防抖和节流
    关于keep-alive的学习
    elementUI上传图片前判断图片的尺寸大小
    vue 判断线上环境还是本地环境
    elementUi table表格的拖拽功能
    获取文件的md5值
    element ui上传腾讯云,更新视频时长
    js音视频文件的时长
    手写一个移动端带惯性的轮播图vue组件
    这个用来总结一些常用的工具函数
  • 原文地址:https://www.cnblogs.com/XBWer/p/2643260.html
Copyright © 2011-2022 走看看