题目链接: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. Struts2标签-checkbox只读属性设置

    Struts2标签-checkbox只读属性设置 在struts2的checkbox标签中,为实现只读效果,一般使用readonly="true"是达不到效果的,但设置disabl ...

  2. 很好的linux下GPIO驱动详解文章

    原文地址  http://blog.csdn.net/llxmedici/article/details/6282372 打算跟着友善之臂的<mini2440 linux移植开发指南>来做 ...

  3. Rmq Problem

    大视野——3339: Rmq Problem Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1192  Solved: 620[Submit][Sta ...

  4. luogu P1043 数字游戏

    题目描述 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易.游戏是这样的,在你面前有一圈整数(一共n个),你要按顺序将其分 ...

  5. Google代码风格配置文件(Java)(IDEA/Eclipse)

    官网:http://www.cnblogs.com/EasonJim/p/7837474.html 下载: 安装: IDEA/Eclipse导入相应文件即可. 说明: Google代码风格文件的缩进是 ...

  6. Linux下使用vi新建文件保存文件时遇到错误:E212: Can't open file for writing

    出现E212: Can't open file for writing的问题是由于权限问题导致的,解决方法有以下思路: 1.使用root进行登录,然后再操作. 2.在使用命令时,前面加sudo. 3. ...

  7. http://www.yiibai.com/java8/java8_temporaladjusters.html

    原文:http://www.yiibai.com/java8/java8_temporaladjusters.html TemporalAdjuster 是做日期数学计算.例如,要获得“本月第二个星期 ...

  8. poj2482--Stars in Your Window(扫描线)

    题目链接:点击打开链接 链接题目大意:给出n个星星的坐标,每一个星星有一个亮度.给出一个矩形的长和宽,问矩形能包含的星星的最大亮度和(不包含边框). 如果每个星星都是矩形的最左下点.那么每个星星都能够 ...

  9. start memcached

    memcached -m 64 -p 11211 -u fly -l 127.0.0.1 ------------------------------------------------------- ...

  10. PHP中extract()函数的妙用

    看cakephp 2.3.8的源代码,很多地方都用 到 compact('name', 'response'); extract($status, EXTR_OVERWRITE); 这样的代码.com ...