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

题意 : 给你一个源点和一个汇点,再给你一些中间边,同时给你他们边上的容量,求从源点到汇点最大流量是多少?
思路分析 :网络流板子题
代码示例 :
using namespace std;
#define ll long long
const int maxn = 205;
const int mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f; int n, m;
struct node
{
int next, v, flow; // flow可以理解为容量限制
}e[maxn<<1];
int cnt;
int head[maxn];
int aim; // 目标点
int deep[maxn]; // 分层图的深度 void addedge(int u, int v, int cap){
e[cnt].v = v;
e[cnt].flow = cap;
e[cnt].next = head[u];
head[u] = cnt++;
} int que[10000]; bool bfs(int s, int t){
memset(deep, 0, sizeof(deep));
deep[s] = 1; que[0] = s; int head1 = 0, tail1 = 1;
while(head1 < tail1){
int u = que[head1++];
for(int i = head[u]; i != -1; i = e[i].next){
int v = e[i].v;
if (!deep[v] && e[i].flow){ // 判断当前的边如果还可以流
deep[v] = deep[u]+1;
que[tail1++] = v;
}
}
}
return deep[t];
} int dfs(int u, int f1){
if (u == aim || f1 == 0) return f1; // 这个优化非常的棒 int f = 0;
for(int i = head[u]; i != -1; i = e[i].next){ // 多路增广,利用dfs的特性
int v = e[i].v;
if (e[i].flow && deep[v] == deep[u]+1){
int x = dfs(e[i].v, min(f1, e[i].flow));
e[i].flow -= x; e[i^1].flow += x;
f1 -= x; f += x;
       if (f1 == 0) return f; // !!!
}
}
if (!f) deep[u] = -2; // 炸点优化,若当前点的流量为 0,则在此次中没有必要再去访问该点了
return f;
} int maxflow(int s, int t){
aim = t; int ret = 0;
cnt = 0;
while(bfs(s, t)){
ret += dfs(s, inf);
}
return ret;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int u, v, w; while(~scanf("%d%d", &n, &m)){
cnt = 0;
memset(head, -1, sizeof(head));
for(int i = 1; i <= n; i++){
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, 0); // 反向边的建立,并赋值 0
}
printf("%d\n", maxflow(1, m));
}
return 0;
}

最大流入门题目 - poj 1273的更多相关文章

  1. UVA 820 --- POJ 1273 最大流

    找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...

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

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

  3. poj 1273 最大流

    题目链接:http://poj.org/problem?id=1273 a.EK算法:(Edmond-Karp): 用BFS不断找增广路径,当找不到增广路径时当前流量即为最大流. b.dinic算法: ...

  4. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  5. poj 1273 Drainage Ditches(最大流)

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

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

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

  7. POJ 1273 网络流(最大流)模板

    http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from ...

  8. (网络流 模板 Dinic) Drainage Ditches --POJ --1273

    链接: http://poj.org/problem?id=1273 代码: //Dinic #include<stdio.h> #include<string.h> #inc ...

  9. (网络流 模板 Edmonds-Karp)Drainage Ditches --POJ --1273

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

随机推荐

  1. P1106 细胞分裂

    题目描述 Hanks博士是BT(Bio-Tech,生物技术)领域的知名专家.现在,他正在为一个细胞实验做准备工作:培养细胞样本. Hanks博士手里现在有 \(N\) 种细胞,编号从 \(1\) 到 ...

  2. P1105 数列

    题目描述 给定一个正整数 \(k(2 \le k \le 15)\) ,把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当 \(k = 3\) 时,这个序列是: 1,3,4, ...

  3. 【js】vue 2.5.1 源码学习 (十) $mount 挂载函数的实现

    大体思路(九) 本节内容: 1. $mount 挂载函数的实现. // 将Vue.prototype.$mount 缓存下来 ==>mountComponet(this,el) { // 组建挂 ...

  4. H3C配置历史命令缓冲大小--接口视图(console为准)

    [wang]user-interface console 0 [wang-ui-console0]history-command max-size 30    //配置缓冲区大小 [wang-ui-c ...

  5. gulp4.0基本配置,超简单!

    最近复习了一下gulp,目前是4.0版本. 下图是基本目录结构,文件里面的内容可以随意添加,超详细简洁啊! 直接上代码(依赖未完全使用): 项目的所有依赖都可以安装,每个都有详细的注释. const ...

  6. 【t044】弗洛伊德

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 弗洛伊德是一个大牛!给一个有向图G,他有n个结点,现在请你求出对于他的每一对结点(x,y),从x出发走 ...

  7. ZOJ——String Successor(字符串模拟题目)

    ZOJ Problem Set - 3490 String Successor Time Limit: 2 Seconds      Memory Limit: 65536 KB The succes ...

  8. Android一般什么情况下会导致内存泄漏

    资料参考:https://blog.csdn.net/u011479990/article/details/78480091 内存泄漏的原因在于生命周期长的对象持有了生命周期短的对象的引用 内存泄漏形 ...

  9. URL统一资源定位符

    URI 是统一资源标识符 URL 是统一资源定位符 ===================================================== 参考链接: 前端学HTTP之URL:ht ...

  10. 用sublime 3搭建php 运行环境

    1.首先把php加入到环境变量中 2.第二步是打开sblime tools->build system->build new system 然后在打开的文件写上 { "cmd&q ...