zoukankan      html  css  js  c++  java
  • poj 1273 -- Drainage Ditches

    Drainage Ditches
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 55017   Accepted: 20992

    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

    最大流的模板题。因为没有clear容器。贡献了好多次wr。。。悲剧。

     1 /*======================================================================
     2  *           Author :   kevin
     3  *         Filename :   DrainageDitches.cpp
     4  *       Creat time :   2014-07-20 17:26
     5  *      Description :
     6 ========================================================================*/
     7 #include <iostream>
     8 #include <algorithm>
     9 #include <cstdio>
    10 #include <cstring>
    11 #include <queue>
    12 #include <cmath>
    13 #define clr(a,b) memset(a,b,sizeof(a))
    14 #define M 205
    15 #define INF 0x7f7f7f7f
    16 using namespace std;
    17 struct Edge{
    18     int from,to,cap,flow;
    19 };
    20 int supers,supert,n,m;
    21 vector<Edge>edges;
    22 vector<int>G[M];
    23 bool vis[M];
    24 int d[M],cur[M];
    25 
    26 bool BFS()
    27 {
    28     clr(vis,0);
    29     queue<int>Q;
    30     Q.push(supers);
    31     d[supers] = 0;
    32     vis[supers] = 1;
    33     while(!Q.empty()){
    34         int x = Q.front(); Q.pop();
    35         int len = G[x].size();
    36         for(int i = 0; i < len; i++){
    37             Edge& e = edges[G[x][i]];
    38             if(!vis[e.to] && e.cap > e.flow){
    39                 vis[e.to] = 1;
    40                 d[e.to] = d[x]+1;
    41                 Q.push(e.to);
    42             }
    43         }
    44     }
    45     return vis[supert];
    46 }
    47 int DFS(int x,int a)
    48 {
    49     if(x == supert || a == 0) return a;
    50     int flow = 0,f;
    51     int len = G[x].size();
    52     for(int& i = cur[x]; i < len; i++){
    53         Edge& e = edges[G[x][i]];
    54         if(d[x] + 1 == d[e.to] && (f = DFS(e.to,min(a,e.cap-e.flow))) > 0){
    55             e.flow += f;
    56             edges[G[x][i]^1].flow -= f;
    57             flow += f;
    58             a -= f;
    59             if(!a) break;
    60         }
    61     }
    62     return flow;
    63 }
    64 void AddEdge(int from,int to,int cap)
    65 {
    66     edges.push_back((Edge){from,to,cap,0});
    67     edges.push_back((Edge){to,from,0,0});
    68     int m = edges.size();
    69     G[from].push_back(m-2);
    70     G[to].push_back(m-1);
    71 }
    72 int Dinic(int s,int t)
    73 {
    74     int flow = 0;
    75     while(BFS()){
    76         clr(cur,0);
    77         flow += DFS(s,INF);
    78     }
    79     return flow;
    80 }
    81 int main(int argc,char *argv[])
    82 {
    83     while(scanf("%d%d",&n,&m)!=EOF){
    84         edges.clear();
    85         for(int i = 0; i < M; i++){
    86             G[i].clear();
    87         }
    88         supers = 1;
    89         supert = m;
    90         int u,v,c;
    91         for(int i = 0; i < n; i++){
    92             scanf("%d%d%d",&u,&v,&c);
    93             AddEdge(u,v,c);
    94         }
    95         int ans = Dinic(supers,supert);
    96         printf("%d
    ",ans);
    97     }
    98     return 0;
    99 }
    View Code
  • 相关阅读:
    如何以nobody用户执行命令?
    记一次全站代理切换----血的教训
    tomcat十大安全优化措施
    paramiko模块使用
    日志分析 第七章 安装grafana
    日志分析 第六章 安装elasticsearch
    日志分析 第五章 安装logstash
    日志分析 第四章 安装filebeat
    IO多路复用及ThreadingTCPServer源码阅读
    socket编程--socket模块介绍
  • 原文地址:https://www.cnblogs.com/ubuntu-kevin/p/3857735.html
Copyright © 2011-2022 走看看