zoukankan      html  css  js  c++  java
  • poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273

    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.

    题意描述:源点为1,汇点为n,源点处是水池,通过水沟排水到汇点河流,问最大排水量。

    算法分析:最大流的模型,每条水沟有最大的排水量,通过建模dinic一遍就ok了。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<vector>
     8 #include<queue>
     9 #define inf 0x7fffffff
    10 using namespace std;
    11 const int maxn=200+10;
    12 
    13 int n,m,from,to;
    14 struct node
    15 {
    16     int v,flow;
    17     int next;
    18 }edge[maxn*4];
    19 int head[maxn],edgenum;
    20 
    21 void add(int u,int v,int flow)
    22 {
    23     edge[edgenum].v=v ;edge[edgenum].flow=flow ;
    24     edge[edgenum].next=head[u] ;head[u]=edgenum++;
    25 
    26     edge[edgenum].v=u ;edge[edgenum].flow=0;
    27     edge[edgenum].next=head[v] ;head[v]=edgenum++;
    28 }
    29 
    30 int d[maxn];
    31 int bfs()
    32 {
    33     memset(d,0,sizeof(d));
    34     d[from]=1;
    35     queue<int> Q;
    36     Q.push(from);
    37     while (!Q.empty())
    38     {
    39         int u=Q.front() ;Q.pop() ;
    40         for (int i=head[u] ;i!=-1 ;i=edge[i].next)
    41         {
    42             int v=edge[i].v;
    43             if (!d[v] && edge[i].flow)
    44             {
    45                 d[v]=d[u]+1;
    46                 Q.push(v);
    47                 if (v==to) return 1;
    48             }
    49         }
    50     }
    51     return 0;
    52 }
    53 
    54 int dfs(int u,int flow)
    55 {
    56     if (u==to || flow==0) return flow;
    57     int cap=flow;
    58     for (int i=head[u] ;i!=-1 ;i=edge[i].next)
    59     {
    60         int v=edge[i].v;
    61         if (d[v]==d[u]+1 && edge[i].flow)
    62         {
    63             int x=dfs(v,min(cap,edge[i].flow));
    64             edge[i].flow -= x;
    65             edge[i^1].flow += x;
    66             cap -= x;
    67             if (cap==0) return flow;
    68         }
    69     }
    70     return flow-cap;
    71 }
    72 
    73 int dinic()
    74 {
    75     int ans=0;
    76     while (bfs()) ans += dfs(from,inf);
    77     return ans;
    78 }
    79 
    80 int main()
    81 {
    82     while (scanf("%d%d",&m,&n)!=EOF)
    83     {
    84         memset(head,-1,sizeof(head));
    85         edgenum=0;
    86         int a,b,c;
    87         for (int i=0 ;i<m ;i++)
    88         {
    89             scanf("%d%d%d",&a,&b,&c);
    90             add(a,b,c);
    91         }
    92         from=1;
    93         to=n;
    94         printf("%d
    ",dinic());
    95     }
    96     return 0;
    97 }
  • 相关阅读:
    RDS 工作笔记
    网站测试需要提供的参数和结果分析
    php 安全编程
    留住青春的格子
    保持工作精力旺盛的方法
    百万格子的标签认领可以提高你在alexa的排名的格子
    老电影,似水流年的记忆
    五行 八字 计算
    iis6.0 的 性能比较
    各种情绪和调节方法
  • 原文地址:https://www.cnblogs.com/huangxf/p/4354330.html
Copyright © 2011-2022 走看看