zoukankan      html  css  js  c++  java
  • hdu1532 Drainage Ditches(最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=1532

    代码来自https://blog.csdn.net/qq_31759205/article/details/52947859

    直接套最大流模板

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int inf=0x3f3f3f3f;
     4 const int maxn=205;
     5 const int maxe=4*maxn*maxn;
     6 struct MaxFlow
     7 {
     8     struct Edge
     9     {
    10         int next,to,w;
    11     }edge[maxe];
    12     int head[maxn],tot,level[maxn];
    13     void init()
    14     {
    15         memset(head,-1,sizeof(head));
    16         tot=0;
    17     }
    18     void add(int u,int v,int w)
    19     {
    20         edge[tot].to=v;
    21         edge[tot].w=w;
    22         edge[tot].next=head[u];
    23         head[u]=tot++;
    24 
    25         edge[tot].to=u;
    26         edge[tot].w=0;
    27         edge[tot].next=head[v];
    28         head[v]=tot++;
    29     }
    30     bool bfs(int s,int t)
    31     {
    32         memset(level,-1,sizeof(level));
    33         queue<int>q;
    34         q.push(s);
    35         level[s]=0;
    36         while(!q.empty())
    37         {
    38             int u=q.front();q.pop();
    39             for(int i=head[u];~i;i=edge[i].next)
    40             {
    41                 int v=edge[i].to;
    42                 if(edge[i].w>0&&level[v]<0)
    43                 {
    44                     level[v]=level[u]+1;
    45                     q.push(v);
    46                 }
    47             }
    48         }
    49         return level[t]>0;
    50     }
    51     int dfs(int u,int t,int f)
    52     {
    53         if(u==t)    return f;
    54         for(int i=head[u];~i;i=edge[i].next)
    55         {
    56             int v=edge[i].to;
    57             if(edge[i].w>0&&level[v]>level[u])
    58             {
    59                 int d=dfs(v,t,min(f,edge[i].w));
    60                 if(d>0)
    61                 {
    62                     edge[i].w-=d;
    63                     edge[i^1].w+=d;
    64                     return d;
    65                 }
    66             }
    67         }
    68         level[u]=-1;
    69         return 0;
    70     }
    71     int slove(int s,int t)
    72     {
    73         int flow=0,f;
    74         while(bfs(s,t))
    75         {
    76             while(f=dfs(s,t,inf))   flow+=f;
    77         }
    78         return flow;
    79     }
    80 }F;
    81 int main()
    82 {
    83     int n,m;
    84     while(~scanf("%d%d",&m,&n))
    85     {
    86         F.init();
    87         for(int i=0;i<m;i++)
    88         {
    89             int u,v,c;
    90             scanf("%d%d%d",&u,&v,&c);
    91             F.add(u,v,c);
    92         }
    93         printf("%d
    ",F.slove(1,n));
    94     }
    95     return 0;
    96 }
    View Code
    如有错误,请指正,感谢!
  • 相关阅读:
    Java 9 模块解耦的设计策略
    Spring Data JPA 事务锁
    如何配置Spring Boot Tomcat
    Spring Cloud Turbine
    Spring Boot 测试时的日志级别
    Spring Boot中使用RSocket
    构造函数
    递归函数的使用
    有序数列的二分搜索
    Java第一次代码作业汇总
  • 原文地址:https://www.cnblogs.com/scott527407973/p/9552669.html
Copyright © 2011-2022 走看看