题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18864    Accepted Submission(s): 8980

Problem 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

题解:

纯最大流。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXN = +; struct Edge
{
int to, next, cap, flow;
}edge[MAXN*MAXN];
int tot, head[MAXN]; int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN]; void add(int u, int v, int w)
{
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = ;
edge[tot].next = head[u]; head[u] = tot++; edge[tot].to = u; edge[tot].cap = ; edge[tot].flow = ;
edge[tot].next = head[v]; head[v] = tot++;
} int sap(int start, int end, int n)
{
memset(gap,,sizeof(gap));
memset(dep,,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -;
gap[] = n;
int maxflow = ;
while(dep[start]<n)
{
bool flag = false;
for(int i = cur[u]; i!=-; i=edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap-edge[i].flow && dep[v]+==dep[u])
{
flag = true;
cur[u] = pre[v] = i;
u = v;
break;
}
} if(flag)
{
if(u==end)
{
int minn = INF;
for(int i = pre[u]; i!=-; i=pre[edge[i^].to])
if(minn>edge[i].cap-edge[i].flow)
minn = edge[i].cap-edge[i].flow;
for(int i = pre[u]; i!=-; i=pre[edge[i^].to])
{
edge[i].flow += minn;
edge[i^].flow -= minn;
}
u = start;
maxflow += minn;
}
} else
{
int minn = n;
for(int i = head[u]; i!=-; i=edge[i].next)
if(edge[i].cap-edge[i].flow && dep[edge[i].to]<minn)
{
minn = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(gap[dep[u]]==) break;
dep[u] = minn+;
gap[dep[u]]++;
if(u!=start) u = edge[pre[u]^].to;
}
}
return maxflow;
} int main()
{
int n, m;
while(scanf("%d%d",&m,&n)!=EOF)
{
tot = ;
memset(head,-,sizeof(head));
for(int i = ; i<=m; i++)
{
int u, v, c;
scanf("%d%d%d",&u,&v,&c);
add(u, v, c);
}
cout<< sap(, n, n) <<endl;
}
}

HDU1532 Drainage Ditches —— 最大流(sap算法)的更多相关文章

  1. HDU 1532 Drainage Ditches(最大流 EK算法)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...

  2. HDU1532 Drainage Ditches 网络流EK算法

    Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...

  3. HDU1532 Drainage Ditches SAP+链式前向星

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

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

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

  5. POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

    http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...

  6. POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. HDU-1532 Drainage Ditches,人生第一道网络流!

    Drainage Ditches 自己拉的专题里面没有这题,网上找博客学习网络流的时候看到闯亮学长的博客然后看到这个网络流入门题!随手一敲WA了几发看讨论区才发现坑点! 本题采用的是Edmonds-K ...

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

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

  9. POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

    Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...

随机推荐

  1. linux 安装软件出现/tmp 磁盘不足时 解决方案

    1.解决办法 mkdir  文件夹——你可以使用的文件夹 比如说 mkdir /mnt/tmp 然后只要export TMPDIR=/mnt/tmp 这样就不会出现 tmp文件夹不够用的情况

  2. LoadBitmap(IDB_BITMAP1) -- 未定义标识符 IDB_BITMAP1

    错误原因:1:头文件没有加入 #include"resource.h" 2:没有导入该资源或者资源ID错误

  3. Mac快速查看隐藏文件

    使用终端 显示隐藏文件的最简单方法是使用终端.只要打开终端(位于应用程序--实用工具),将以下代码复制进去然后回车 defaults write com.apple.finder AppleShowA ...

  4. git status检测不到文件变化

    SourceTree(Git)无法检测新增文件的解决方法 有时候使用git管理软件SourceTree会遇到往项目里新增了文件,软件却没有任何反应的问题,这多发生在git合并出错而只能重新git的情况 ...

  5. 使用EventNext实现基于事件驱动的业务处理

    事件驱动模型相信对大家来说并不陌生,因为这是一套非常高效的逻辑处理模型,通过事件来驱动接下来需要完成的工作,而不像传统同步模型等待任务完成后再继续!虽然事件驱动有着这样的好处,但在传统设计上基于消息回 ...

  6. 洛谷——P1025 数的划分

    P1025 数的划分 题目描述 将整数n分成k份,且每份不能为空,任意两个方案不相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1; 问有 ...

  7. List 与 ArrayList 的使用

    最近回顾 java 集合,发现大部分程序中都在使用 List list = new ArrayList(); 也有部分程序使用 ArrayList list = new ArrayList(); 那么 ...

  8. javaScript 翻转

    一个字符串转成如下形式: 一个字符串转成如下形式"olleh dlrow"; public class reverseWord { public static void main( ...

  9. 自动调整文字高度With what should I replace the deprecated sizeWithFont:contrainedToSize:lineBreakMode method?

    自动调整文字的高度: ios 2.0 ~ 7.0以下: UILabel *orgnizationLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, ...

  10. Weblogic调优

    优化说明: 一.Weblogic服务程序设置: 1.设置JDK内存: 修改weblogic\user_projects\domains\base_domain\bin下的setDomainEnv.cm ...