• POJ Drainage Ditches


    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

    Source

     
    题目给你m条边 n个点 求1-n的最大流
    模板练手
     
     1 /*
     2     最大流EK算法
     3     模板练手 
     4 */
     5 #include<queue>
     6 #include<cstdio>
     7 #include<cstring>
     8 #include<iostream>
     9 #define MAXN 210
    10 #define INF 1e9
    11 
    12 using namespace std;
    13 
    14 int c[MAXN][MAXN];
    15 
    16 int n,m,x,y,val;
    17 
    18 int pre[MAXN];
    19 
    20 queue<int> q;
    21 
    22 bool vis[MAXN];
    23 
    24 inline void read(int&x) {
    25     int f=1;x=0;char c=getchar();
    26     while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
    27     while(c>='0'&&c<='9') {x=(x<<1)+(x<<3)+c-48;c=getchar();}
    28     x=x*f;
    29 }
    30 
    31 inline bool bfs(int start,int end) {
    32     fill(pre,pre+1+MAXN,-1);
    33     fill(vis,vis+1+MAXN,false);
    34     pre[start]=0;
    35     vis[start]=true;
    36     while(!q.empty()) q.pop();
    37     q.push(start);
    38     while(!q.empty()) {
    39         int u=q.front();
    40         q.pop();
    41         for(int i=1;i<=n;i++) {
    42             if(i!=u&&c[u][i]&&!vis[i]) {
    43                 pre[i]=u;
    44                 vis[i]=true;
    45                 if(i==end) return true;
    46                 q.push(i);
    47             }
    48         }
    49     }
    50     return false;
    51 }
    52 
    53 inline int maxFLOW(int st,int end) {
    54     int flow=0;
    55     while(bfs(st,end)) {
    56         int t=INF;
    57         for(int i=end;i!=st;i=pre[i])
    58           t=min(t,c[pre[i]][i]);
    59         for(int i=end;i!=st;i=pre[i]) {
    60             c[pre[i]][i]-=t;
    61             c[i][pre[i]]+=t;
    62         }
    63         flow+=t;
    64     }
    65     return flow;
    66 }
    67 
    68 int main() {
    69     while(~scanf("%d%d",&m,&n)) {
    70         memset(c,0,sizeof c);
    71         for(int i=0;i<m;i++) {
    72             read(x);read(y);read(val);
    73             c[x][y]+=val;
    74         }
    75         printf("%d
    ",maxFLOW(1,n));
    76     }
    77     return 0;
    78 }
    代码


    作者:乌鸦坐飞机
    出处:http://www.cnblogs.com/whistle13326/
    新的风暴已经出现 怎么能够停止不前 穿越时空 竭尽全力 我会来到你身边 微笑面对危险 梦想成真不会遥远 鼓起勇气 坚定向前 奇迹一定会出现

     
  • 相关阅读:
    css深入理解absolute
    CSS深入理解float
    SpringBoot连接Oracle
    Oralce分页
    ps
    VUE基本安装
    JAVA运行war包
    MYSQL数据库事务隔离级别
    如何设计一个关系型数据库
    省选模拟22
  • 原文地址:https://www.cnblogs.com/whistle13326/p/7191533.html
Copyright © 2020-2023  润新知