zoukankan      html  css  js  c++  java
  • HDU1532 Drainage Ditches —— 最大流(sap算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532


    Drainage Ditches

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 18864    Accepted Submission(s): 8980

    Problem 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


    题解:

    纯最大流。


    代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 #define ms(a,b) memset((a),(b),sizeof((a)))
     13 using namespace std;
     14 typedef long long LL;
     15 const int INF = 2e9;
     16 const LL LNF = 9e18;
     17 const int mod = 1e9+7;
     18 const int MAXN = 500+10;
     19 
     20 struct Edge
     21 {
     22     int to, next, cap, flow;
     23 }edge[MAXN*MAXN];
     24 int tot, head[MAXN];
     25 
     26 int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN];
     27 
     28 void add(int u, int v, int w)
     29 {
     30     edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0;
     31     edge[tot].next = head[u]; head[u] = tot++;
     32     
     33     edge[tot].to = u; edge[tot].cap = 0; edge[tot].flow = 0;
     34     edge[tot].next = head[v]; head[v] = tot++;
     35 }
     36 
     37 int sap(int start, int end, int n)
     38 {
     39     memset(gap,0,sizeof(gap));
     40     memset(dep,0,sizeof(dep));
     41     memcpy(cur,head,sizeof(head));
     42     int u = start;
     43     pre[u] = -1;
     44     gap[0] = n;
     45     int maxflow = 0;
     46     while(dep[start]<n)
     47     {
     48         bool flag = false;
     49         for(int i = cur[u]; i!=-1; i=edge[i].next)
     50         {
     51             int v = edge[i].to;
     52             if(edge[i].cap-edge[i].flow && dep[v]+1==dep[u])
     53             {
     54                 flag = true;
     55                 cur[u] = pre[v] = i;
     56                 u = v;
     57                 break;
     58             }
     59         }
     60 
     61         if(flag)
     62         {
     63             if(u==end)
     64             {
     65                 int minn = INF;
     66                 for(int i = pre[u]; i!=-1; i=pre[edge[i^1].to])
     67                     if(minn>edge[i].cap-edge[i].flow)
     68                         minn = edge[i].cap-edge[i].flow;
     69                 for(int i = pre[u]; i!=-1; i=pre[edge[i^1].to])
     70                 {
     71                     edge[i].flow += minn;
     72                     edge[i^1].flow -= minn;
     73                 }
     74                 u = start;
     75                 maxflow += minn;
     76             }
     77         }
     78 
     79         else
     80         {
     81             int minn = n;
     82             for(int i = head[u]; i!=-1; i=edge[i].next)
     83                 if(edge[i].cap-edge[i].flow && dep[edge[i].to]<minn)
     84                 {
     85                     minn = dep[edge[i].to];
     86                     cur[u] = i;
     87                 }
     88             gap[dep[u]]--;
     89             if(gap[dep[u]]==0) break;
     90             dep[u] = minn+1;
     91             gap[dep[u]]++;
     92             if(u!=start) u = edge[pre[u]^1].to;
     93         }
     94     }
     95     return maxflow;
     96 }
     97 
     98 int main()
     99 {
    100     int n, m;
    101     while(scanf("%d%d",&m,&n)!=EOF)
    102     {
    103         tot = 0;
    104         memset(head,-1,sizeof(head));
    105         for(int i = 1; i<=m; i++)
    106         {
    107             int u, v, c;
    108             scanf("%d%d%d",&u,&v,&c);
    109             add(u, v, c);
    110         }
    111         cout<< sap(1, n, n) <<endl;
    112     }
    113 }
    View Code
  • 相关阅读:
    数据库数据闪回设置
    Ajax
    Spring Framework & Spring Boot
    Maven与Ant比较
    Spring中控制反转(IoC)/依赖注入(DI)的区别
    C# 图片加水印、截取图片、压缩图片等。有个坑,往下看。当图片为jpg格式时,发现出来的图片结果异常的大
    json 递归
    Webservice 问题:system.argumentexception:找不到xxx定义。缺少命名空间为xxx的服务说明
    利用.net 自带的工具生成webservice服务(windows)
    sqlserver 查询 今天明天后天等数据
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538610.html
Copyright © 2011-2022 走看看