zoukankan      html  css  js  c++  java
  • CODEVS-1993 草地排水

    题目描述 Description

    在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水。这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间。因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没的烦恼(不用担心,雨水会流向附近的一条小溪)。作为一名一流的技师,农夫约翰已经在每条排水沟的一端安上了控制器,这样他可以控制流入排水沟的水流量。

    农夫约翰知道每一条排水沟每分钟可以流过的水量,和排水系统的准确布局(起点为水潭而终点为小溪的一张网)。需要注意的是,有些时候从一处到另一处不只有一条排水沟。

    根据这些信息,计算从水潭排水到小溪的最大流量。对于给出的每条排水沟,雨水只能沿着一个方向流动,注意可能会出现雨水环形流动的情形。

    输入描述 Input Description

    第1行: 两个用空格分开的整数N (0 <= N <= 200) 和 M (2 <= M <= 200)。N是农夫John已经挖好的排水沟的数量,M是排水沟交叉点的数量。交点1是水潭,交点M是小溪。

    第二行到第N+1行: 每行有三个整数,Si, Ei, 和 Ci。Si 和 Ei (1 <= Si, Ei <= M) 指明排水沟两端的交点,雨水从Si 流向Ei。Ci (0 <= Ci <= 10,000,000)是这条排水沟的最大容量。

    输出描述 Output Description

    输出一个整数,即排水的最大流量。

    样例输入 Sample Input
    5 4
    1 2 40
    1 4 20
    2 4 20
    2 3 30
    3 4 10
    样例输出 Sample Output

    50

    水道模板题。。。黄学长的模板好用!

     1 #include<iostream>
     2 #include<cstring>
     3 #include<queue>
     4 using namespace std;
     5 
     6 struct data
     7 {
     8     int to,w,next;
     9 }E[6000001];
    10 int n,m,node=0,ans=0;
    11 int head[1000001]={0},h[1000001]={0},q[1000001]={0};
    12 
    13 void insert(int u,int v,int w)
    14 {
    15     node++;
    16     E[node].to=v;
    17     E[node].w=w;
    18     E[node].next=head[u];
    19     head[u]=node;
    20 }
    21 
    22 bool bfs()
    23 {
    24     int i;
    25     memset(h,-1,sizeof(h));
    26     queue<int> Q;
    27     Q.push(1);
    28     h[1]=0;
    29     while(!Q.empty())
    30     {
    31         int p=Q.front();
    32         Q.pop();
    33         i=head[p];
    34         while(i)
    35         {
    36             if(E[i].w&&h[E[i].to]<0)
    37             {
    38                 Q.push(E[i].to);
    39                 h[E[i].to]=h[p]+1;
    40             }
    41             i=E[i].next;
    42         }
    43     }
    44     if(h[m]==-1) return 0;
    45     return 1;
    46 }
    47 
    48 int dfs(int x,int f)
    49 {
    50     if(x==m) return f;
    51     int i=head[x];
    52     int w,used=0;
    53     while(i)
    54     {
    55         if(E[i].w&&h[E[i].to]==h[x]+1)
    56         {
    57             w=f-used;
    58             w=dfs(E[i].to,min(w,E[i].w));
    59             E[i].w-=w;
    60             E[i+1].w+=w;
    61             used+=w;
    62             if(used==f) return f;
    63         }
    64         i=E[i].next;
    65     }
    66     if(!used) h[x]=-1;
    67     return used;
    68 }
    69 
    70 void dinic()
    71 {
    72     while(bfs()) ans+=dfs(1,0x7fffffff);
    73 }
    74 
    75 int main()
    76 {
    77     cin>>n>>m;
    78     for(int i=1;i<=n;i++)
    79     {
    80         int x,y,z;
    81         cin>>x>>y>>z;
    82         insert(x,y,z);
    83         insert(y,x,z);
    84     }
    85     dinic();
    86     cout<<ans<<endl;
    87     return 0;
    88 }
  • 相关阅读:
    python之模块与包
    python之异常处理
    python之os与json&pickle模块
    python之random、time与sys模块
    python之re模块
    python之匿名函数、递归与二分法
    python之内置函数
    python之迭代器、生成器及列表推导式
    python之第一对象,函数名的应用,闭包
    python之命名空间与作用域
  • 原文地址:https://www.cnblogs.com/InWILL/p/5918364.html
Copyright © 2011-2022 走看看