题目链接: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. jenkins执行自动化用例(详细、有用、mark 优先级高高高)

    http://blog.sina.com.cn/s/blog_68f262210102vx8o.html 第七章 测试用例接入jenkins自动运行 ------Web自动化测试之Webdriver+ ...

  2. Bichrome Tree

    Bichrome Tree 时间限制: 1 Sec  内存限制: 128 MB 题目描述 We have a tree with N vertices. Vertex 1 is the root of ...

  3. 淘金(bzoj 3131)

    Description 小Z在玩一个叫做<淘金者>的游戏.游戏的世界是一个二维坐标.X轴.Y轴坐标范围均为1..N.初始的时候,所有的整数坐标点上均有一块金子,共N*N块.    一阵风吹 ...

  4. msp430项目编程51

    msp430综合项目---扩展项目一51 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结

  5. CentOS配置DHCP服务器

    知识储备 bootp (boot protocol) 早前用于无盘工作站,dhcp的前身 IP初次分配完成,以后固定mac和IP绑定关系 dhcp基础 获取IP步骤 step1: Client dhc ...

  6. Maven实战:Pom.xml详解

    什么是pom?    pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的u ...

  7. python和python-dev

    问:python-dev是什么?为什么安装了python后有时还要安装python-dev? 答: linux发行版通常会把类库的头文件和相关的pkg-config分拆成一个单独的xxx-dev(el ...

  8. AbstractQueuedSynchronizer 队列同步器源码分析

    AbstractQueuedSynchronizer 队列同步器(AQS) 队列同步器 (AQS), 是用来构建锁或其他同步组件的基础框架,它通过使用 int 变量表示同步状态,通过内置的 FIFO ...

  9. Spring在Bean中注入集合

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/injecting-collection.html: 如果你想传递多个值,如Java Collect ...

  10. amplab

    https://github.com/amplab/SparkNet https://amplab.cs.berkeley.edu/