Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 70588   Accepted: 27436

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

[Submit]   [Go Back]   [Status]  
[Discuss]

解题思路:

是一道裸的一般增广路算法的题目,就是计算水塘最大的流量排到附近的小河中。

增广路中可能含有反向弧,最后流量改进的时候对于反向弧的处理,既可以按照残留网络中的对应的正向弧那样增加流量也可以直接把这条反向弧的流量减少相应的流量。

因为我用这两种解法都能AC,说明效果是一样的。

源代码:

<span style="font-size:18px;">
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; #define MAXN 210 struct Matrix
{
int c,f;//容量,流量
};
Matrix Edge[MAXN][MAXN];//流及容量(邻接矩阵)
int M,N;//弧的条数,顶点个数
int s,t;//源点(1),汇点(n)
int residul[MAXN][MAXN];//残留网络
int qu[MAXN*MAXN],qs,qe;//队列、队列头、队列尾
int pre[MAXN];//pre[i]为增广路上顶点的访问标志
int vis[MAXN];//BFS算法中各个顶点的访问标志
int maxflow,min_augment;//最大流流量、每次增广时的可以改进的量
void find_augment_path()//BFS寻找增广路
{
int i,cu;//cu为队列头顶点
memset(vis,0,sizeof(vis));
qs=0;qu[qs]=s;//s入队列
pre[s]=s;vis[s]=1;qe=1;
memset(residul,0,sizeof(residul));memset(pre,0,sizeof(pre));
while(qs<qe&&pre[t]==0)
{
cu=qu[qs];
for(i=1;i<=N;i++)
{
if(vis[i]==0)
{
if(Edge[cu][i].c-Edge[cu][i].f>0)
{
residul[cu][i]=Edge[cu][i].c-Edge[cu][i].f;
pre[i]=cu;qu[qe++]=i;vis[i]=1;
}
else if(Edge[i][cu].f>0)
{
residul[cu][i]=Edge[i][cu].f;
pre[i]=cu;qu[qe++]=i;vis[i]=1;
}
}
}
qs++;
}
}
void augment_flow()//计算可改进量
{
int i=t,j;//t为汇点
if(pre[i]==0)
{
min_augment=0;return;
}
j=0x7fffffff;
while(i!=s)//计算增广路上可改进量的最小值
{
if(residul[pre[i]][i]<j) j=residul[pre[i]][i];
i=pre[i];
}
min_augment=j;
}
void update_flow()//调整流量
{
int i=t;//t为汇点
if(pre[i]==0)return;
while(i!=s)
{
if(Edge[pre[i]][i].c-Edge[pre[i]][i].f>0)
Edge[pre[i]][i].f+=min_augment;
else if(Edge[i][pre[i]].f>0) Edge[pre[i]][i].f+=min_augment;//或者写成Edge[i][pre[i]]-=min_augment;
i=pre[i];
}
}
void solve()
{
s=1;t=N;
maxflow=0;
while(1)
{
find_augment_path();//BFS寻找增广路
augment_flow();//计算可改进量
maxflow+=min_augment;
if(min_augment>0) update_flow();
else return;
}
}
int main()
{
int i;
int u,v,c;
while(scanf("%d %d",&M,&N)!=EOF)
{
memset(Edge,0,sizeof(Edge));
for(i=0;i<M;i++)
{
scanf("%d %d %d",&u,&v,&c);
Edge[u][v].c+=c;
}
solve();
printf("%d\n",maxflow);
}
return 0;
}
</span>

POJ-1273-Drainage Ditches 朴素增广路的更多相关文章

  1. poj 1273 Drainage Ditches(最大流)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  2. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  3. POJ 1273 Drainage Ditches (网络最大流)

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  4. POJ 1273 Drainage Ditches题解——S.B.S.

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67823   Accepted: 2620 ...

  5. POJ 1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67387   Accepted: 2603 ...

  6. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  7. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

  8. 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches

    Drainage Ditches 题目抽象:给你m条边u,v,c.   n个定点,源点1,汇点n.求最大流.  最好的入门题,各种算法都可以拿来练习 (1):  一般增广路算法  ford() #in ...

  9. POJ 1273 Drainage Ditches【最大流模版】

    题意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条有向水渠,给出这n条水渠所连接的点和所能流过的最大流量,求从源点到汇点能流过的最大流量 Dinic #include<iost ...

随机推荐

  1. RabbitMQ使用详解

    刚刚用了,记录下来,以后忘了,方便能够快速想起来. 首先说明,由于RabbitMQ服务端非JAVA,C++语言,当然也就看不懂,所以本文的理解都是过于主观的. 一,RabbitMQ服务端搭建 推荐最好 ...

  2. ngRoute路径出现#!#解决方案

    在做一个开源项目的时候,使用了"angular-route": "^1.6.4",发现设置了<a>标签的href后,点击后路径出现的不是#/,而是# ...

  3. (转)微信开发连接SAE数据库

    原文地址:http://blog.csdn.net/tterminator/article/details/51067130 在读写云数据库MySQL之前,需要说明的是,在新浪云平台上使用数据库时,该 ...

  4. copy11

    方法二 这种方法也比较简单,主要针对你没有.apk包的情况,比如Android原生自带的APP(计算器.通讯录.短信...),可以通过adb 命令. 1,打开APP. 2,执行> adb log ...

  5. Maven依赖解析

    本文将记录Maven工程中依赖解析机制,内容包括: Maven依赖基本结构 从仓库解析依赖的机制 依赖传递性解析实例 1. Maven依赖基本结构 上篇文章记录了Maven依赖的聚合与继承,POM中依 ...

  6. 基于.NET CORE微服务框架 -surging 基于messagepack、protobuffer、json.net 性能对比

    1.前言 surging内部使用的是高性能RPC远程服务调用,如果用json.net序列化肯定性能上达不到最优,所以后面扩展了protobuf,messagepack序列化组件,以支持RPC二进制传输 ...

  7. 四、VueJs 填坑日记之搭建Axios接口请求工具

    上一章,我们认识了项目的目录结构,以及对项目的目录结构做了一些调整,已经能把项目重新跑起来了.今天我们来搭建api接口调用工具Axios.Vue本身是不支持ajax调用的,如果你需要这些功能就需要安装 ...

  8. Api管理工具(spring-rest-docs)

    对于app开发来说,必须需要有相应的api文档,一般最基础的就是用markdown工具来撰写api文档.当对于开发人员来说,是总会想着寻找更方便撰写,测试,对接前端开发的文档生成的工具. 其实这方面的 ...

  9. 【小技巧解决大问题】使用 frp 突破阿里云主机无弹性公网 IP 不能用作 Web 服务器的限制

    背景 今年 8 月份左右,打折价买了一个阿里云主机,比平常便宜了 2000 多块.买了之后,本想作为一个博客网站的,毕竟国内的服务器访问肯定快一些.满心欢喜的下单之后,却发现 http 服务,外网怎么 ...

  10. StackExchange.Redis学习笔记(四) 事务控制和Batch批量操作

    Redis事物 Redis命令实现事务 Redis的事物包含在multi和exec(执行)或者discard(回滚)命令中 和sql事务不同的是,Redis调用Exec只是将所有的命令变成一个单元一起 ...