Drainage Ditches
 
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 91824   Accepted: 35588

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

 
思路:给出点之间的容量,求整个网络的最大流,最大流板子题,这里用的是ISAP模板。
 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector> using namespace std;
const int maxnE = 1e6 + ;
const int maxn = 1e5 + ;
const int maxnQ = 1e6 + ;
const int inf = 0x3f3f3f3f; struct edge {
int v;///弧尾
int cap;///容量
int nxt;///指向下一条从同一个弧头出发的弧
}e[maxnE]; int head[maxn],cnt;
int d[maxn],cur[maxn],pre[maxn],num[maxn];
int source,sink;///超级源、超级汇
int nv;///编号修改的上限
int n,m; queue <int> q; void add(int u, int v, int capacity) {
e[cnt].v = v;
e[cnt].cap = capacity;
e[cnt].nxt = head[u];
head[u] = cnt++;
//正向边 e[cnt].v = u;
e[cnt].cap = ;
e[cnt].nxt = head[v];
head[v] = cnt++;
//反向边
} void rev_bfs() {///反向bfs
memset(num, , sizeof(num));
memset(d, -, sizeof(d));
d[sink] = ;///超级汇直接标记
num[] = ;
q.push(sink);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if(~d[v]) continue;///已经标过号
d[v] = d[u] + ;
q.push(v);
num[d[v]]++;
}
}
} int ISAP() {
memcpy(cur, head, sizeof(cur));///当前弧优化
rev_bfs();
int flow = , u = pre[source] = source;
int i;
while(d[sink] < nv) {///最长的一条链上,最大的下标是nv-1,如果大于等于nv说明已断层
//printf("flow:%d\n",flow);
if(u == sink) {///如果找到一条增广路,则沿着此条路修改flow
int f = inf, neck;
for(i = source; i != sink; i = e[cur[i]].v) {///修改流量
if(f > e[cur[i]].cap) {
f = e[cur[i]].cap;///不断减少所需要的流量
neck = i;///记录回退点,不用回到起点再找
}
}
for(i = source; i != sink; i = e[cur[i]].v) {///修改流量
e[cur[i]].cap -= f;
e[cur[i] ^ ].cap += f;
}
flow += f;
u = neck;///回退
}
for(i = cur[u]; ~i; i = e[i].nxt) {
if(d[e[i].v] + == d[u] && e[i].cap) break;
}
if(~i) {
//如果存在可行的增广路
cur[u] = i;
pre[e[i].v] = u;
u = e[i].v;
} else {///否则回退,重新找增广路
if( == (--num[d[u]])) break;
int mind = nv;
for(i = head[u]; ~i; i = e[i].nxt) {
if(e[i].cap && mind > d[e[i].v]) {///寻找可以增广的最小下标
cur[u] = i;
mind = d[e[i].v];
}
}
d[u] = mind + ;
num[d[u]]++;
u = pre[u];///回退
}
}
return flow;
} void init() {///初始化
memset(head, -, sizeof(head));
cnt = ;
} void solve()
{
int u,v,c;
init();
for(int i = ; i < m; ++i) {
scanf("%d %d %d",&u, &v, &c);
add(u,v,c);
}
source = , sink = n, nv = sink + ;
printf("%d\n",ISAP());
} int main()
{
while(scanf("%d %d", &m, &n) != EOF) {
solve();
}
return ;
}

POJ1273【网络流】的更多相关文章

  1. POJ1273 网络流-->最大流-->模板级别-->最大流常用算法总结

    一般预流推进算法: 算法思想: 对容量网络G 的一个预流f,如果存在活跃顶点,则说明该预流不是可行流. 预流推进算法就是要选择活跃顶点,并通过它把一定的流量推进到它的邻接顶点,尽可能将正的赢余减少为0 ...

  2. 【生活没有希望】poj1273网络流大水题

    你不能把数据规模改大点吗= =我优化都不加都过了 #include <cstdio> #define INF 2147483647 int n,m,ans,x,y,z,M; ],l[],f ...

  3. Drainage Ditches(POJ1273+网络流+Dinic+EK)

    题目链接:poj.org/problem?id=1273 题目: 题意:求最大流. 思路:测板子题,分别用Dinic和EK实现(我的板子跑得时间均为0ms). Dinic代码实现如下: #includ ...

  4. poj1273 网络流入门题 dinic算法解决,可作模板使用

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62078   Accepted: 2384 ...

  5. 最大流算法-ISAP

    引入 最大流算法分为两类,一种是增广路算法,一种是预留推进算法.增广路算法包括时间复杂度\(O(nm^2)\)的EK算法,上界为\(O(n^2m)\)的Dinic算法,以及一些其他的算法.EK算法直接 ...

  6. ACM/ICPC 之 网络流入门-EK算法(参考模板)(POJ1273)

    基于残留网络与FF算法的改进-EK算法,核心是将一条边的单向残留容量的减少看做反向残留流量的增加. //网络流 //EK算法 //Time:16Ms Memory:348K #include<i ...

  7. 【网络流】POJ1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78671   Accepted: 3068 ...

  8. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

    题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...

  9. 网络流相关知识点以及题目//POJ1273 POJ 3436 POJ2112 POJ 1149

    首先来认识一下网络流中最大流的问题 给定一个有向图G=(V,E),把图中的边看做成管道,边权看做成每根管道能通过的最大流量(容量),给定源点s和汇点t,在源点有一个水源,在汇点有一个蓄水池,问s-t的 ...

  10. POJ1273 USACO 4.2.1 Drainage Ditches CodeVS1993草地排水 网络流 最大流 SAP

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 传送门 - POJ 传送门 - CodeVS 题意概括 给出一个图,告诉你边和容量,起点是1,汇点是n,让你求最大流. 题解 ...

随机推荐

  1. 《自拍教程29》Sublime_小脚本编写首选

    Sublime Sublime 是一个轻量.简洁.高效.跨平台的编辑器, 最新的是Sublime Text 3. Sublime对Python支持非常好,如果只是简单的编写批处理脚本编写, 或者小范围 ...

  2. ES6 - 基础学习(3): 变量的解构赋值

    解构赋值概述 1.解构赋值是对赋值运算符的扩展. 2.它是一种针对数组或者对象进行模式匹配,然后对其中的变量进行赋值. 3.代码书写上显得简洁且易读,语义更加清晰明了:而且还方便获取复杂对象中的数据字 ...

  3. BOS只读状态修改

    update T_META_OBJECTTYPE set FSUPPLIERNAME ='PAEZ',FPACKAGEID =null

  4. Premiere Pro CC2018安装教程

    Premiere Pro CC2018安装教程 下载安装包:去官网下载或者百度PR2018的安装包 解压安装包后,找到Set-up.exe,右键,打开 安装的时候我们需要注册一个账号,点击“获取Ado ...

  5. ts中的装饰器

    // 装饰器一种特殊的类的声明, 扩展类.属性.方法. function logClass(params:any) { console.log(params); // params代表HttpClic ...

  6. html网页基本结构

    <!DOCTYPE> 不是 HTML 标签.它为浏览器提供一项信息(声明),即 HTML 是用什么版本编写的. HTML5 DOCTYPE 的 HTML 文档类型如下: <!DOCT ...

  7. AI Web 2.0

    kali: 192.168.0.103 目标机:192.168.0.105 0X01 扫描端口和目录 a)扫描端口 开启了80和22端口 b)扫描目录 看到两个敏感字样的目录 尝试访问/webadmi ...

  8. Mac搭建本地服务器并映射到外网

    最近在学习Html,小有进步变想着写一个浪漫的静态页面给女朋友浪漫一下,那么问题就来了,如何把我的网页让对网络一窍不通的女朋友看到,所以便想到了是用自己电脑作为服务器的想法.百度以后整理如下: 首先搭 ...

  9. GHM论文笔记(CVPR2019)

    目录 作者要解决的问题 Focal loss(CVPR2017) Focal loss的解决方案 Focal loss的不足 设计思路 梯度与样本的关系 梯度分布计算方法:将0-1的梯度切bin,计算 ...

  10. 快速读写模板(int)

    一.快速读入模板(int) inline int read(int x){ char ch=getchar(); int x=0,f=1; while(ch>='9'||ch<='0'){ ...