• HDU1532 Drainage Ditches SAP+链式前向星


    Drainage Ditches

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


    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
     
    Sample Output
    50
     思路:裸SAP,注意建边的过程。三种优化请忽略,验证模板
    代码:
     1 #include<iostream>
     2 #include<string>
     3 #include<algorithm>
     4 #include<cstdio>
     5 #include<cstring>
     6 #include<cstdlib>
     7 #include<cmath>
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 const int maxn=205;
    11 const int maxm=maxn*maxn;
    12 struct edgenode {
    13     int c,next,to;
    14 }edges[maxm];
    15 int head[maxn];//链式前向星
    16 int index;
    17 int sapmaxflow(int source, int sink, int n) {
    18     //numh:用于GAP优化的统计高度数量数组;h:距离标号数组;curedges:当前弧数组;pre:前驱数组
    19     int numh[maxn],h[maxn],curedges[maxn],pre[maxn];
    20     //初始化最大流为0
    21     int cur_flow,flow_ans=0,u,tmp,neck,i;
    22     memset(h,0,sizeof(h));
    23     memset(numh,0,sizeof(numh));
    24     memset(pre,-1,sizeof(pre));
    25     //初始化当前弧为第一条邻接边
    26     for(i=1;i<=n;++i) curedges[i]=head[i];
    27     numh[0]=n;
    28     u=source;
    29     //当h[start]>=n时,网络中能够肯定出现了GAP
    30     while(h[source]<n) {
    31         if(u==sink) {
    32             cur_flow=INF;
    33             //增广成功,寻找“瓶颈”边
    34             for(i=source;i!=sink;i=edges[curedges[i]].to) {
    35                 if(cur_flow>edges[curedges[i]].c) {
    36                     neck=i;
    37                     cur_flow=edges[curedges[i]].c;
    38                 }
    39             }
    40             //修改路径上的容量
    41             for(i=source;i!=sink;i=edges[curedges[i]].to) {
    42                 tmp=curedges[i];
    43                 edges[tmp].c-=cur_flow;
    44                 edges[tmp^1].c+=cur_flow;
    45             }
    46             flow_ans+=cur_flow;
    47             //下次增广从瓶颈边开始
    48             u=neck;
    49         }
    50         for(i=curedges[u];i!=-1;i=edges[i].next) if(edges[i].c&&h[u]==h[edges[i].to]+1) break;
    51         if(i!=-1) {
    52             curedges[u]=i;
    53             pre[edges[i].to]=u;
    54             u=edges[i].to;
    55         } else {
    56             //GAP优化
    57             if(0==--numh[h[u]]) break;
    58             curedges[u]=head[u];
    59             for(tmp=n,i=head[u];i!=-1;i=edges[i].next) if(edges[i].c) tmp=min(tmp,h[edges[i].to]);
    60             //重新标号并且从当前点前驱重新增广
    61             h[u]=tmp+1;
    62             ++numh[h[u]];
    63             if(u!=source) u=pre[u];
    64         }
    65     }
    66     return flow_ans;
    67 }
    68 void addedge(int u, int v, int c) {
    69     edges[index].to=v;
    70     edges[index].c=c;
    71     edges[index].next=head[u];
    72     head[u]=index++;
    73     edges[index].to=u;
    74     edges[index].c=0;
    75     edges[index].next=head[v];
    76     head[v]=index++;
    77 }
    78 int main() {
    79     int n,m;
    80     while(~scanf("%d%d",&m,&n)) {
    81         int u,v,c;
    82         for(int i=0;i<maxm;++i) edges[i].next=-1;
    83         for(int i=0;i<maxn;++i) head[i]=-1;
    84         index=0;
    85         for(int i=0;i<m;++i) {
    86             scanf("%d%d%d",&u,&v,&c);
    87             addedge(u,v,c);
    88         }
    89         printf("%d
    ",sapmaxflow(1,n,n));
    90     }
    91     return 0;
    92 }
    View Code
  • 相关阅读:
    Mybatis学习-ResultMap
    MySql模糊查询 concat()函数
    Spring学习-依赖注入
    Struts2学习-struts执行过程简述
    Struts2学习-jsp中超链接传参问题
    Struts2学习-struts.xml文件配置
    第四次作业
    第三次作业
    Django -Ajax
    Django -ORM
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7775975.html
Copyright © 2020-2023  润新知