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. 寒假答辩作品:Java小游戏

    目录 java入门小游戏[test] 游戏界面 前言 (可直接跳到程序介绍) 前期入门小项目 前期收获 后期自创关卡 后续 java入门小游戏[test] 游戏界面 github地址: https:/ ...

  2. 基于已构建S2SH项目配置全注解方式简化配置文件

    如果还不熟悉s2sh项目搭建的朋友可以先阅读 eclipse环境下基于tomcat-7.0.82构建struts2项目 eclipse环境下基于已构建struts2项目整合spring+hiberna ...

  3. Pyinstaller打包exe,丢失图标等问题

    Pyinstaller打包exe,丢失图标等问题 一.原因 exe运行时会解压一个名为'_MEI*'的资源文件夹到电脑的临时目录,程序结束时删除. 程序里使用'\图标.png'这样的路径,exe运行时 ...

  4. OpenCV3入门(十)图像轮廓

    1.图像轮廓 1.1图像轮廓与API函数 轮廓是一系列相连的点组成的曲线,代表了物体的基本外形,相对于边缘,轮廓是连续的,边缘并不全部连续.一般地,获取图像轮廓要经过下面几个步骤: 1)     读取 ...

  5. sublime3 快捷键总结

    配置环境:win7 sublime3 1,快速html5模板页(已安装Emmet插件) 输入html:5后,然后 ctrl+e 2,快速复制 ctrl+shift+d 复制光标所在当前行 3,向上/向 ...

  6. 复杂系统架构设计<1>

    这两天开始读由Edward Crawley(爱德华 克劳利).Bruce Cameron(布鲁斯 卡梅隆).Daniel Selva(丹尼尔 塞尔瓦)著作的系统架构,一开始看目录以为是介绍系统软件架构 ...

  7. pip 自己的源 搭建

    1  安装工具 pip install  pip2pi 2  下载 所需要的包 pip2tgz /application/nginx/html/yum/python/ apscheduler (172 ...

  8. java 企业 网站源码 模版 屏幕自适应 有前后台 springmvc SSM 生成静态化

    前台: 支持四套模版, 可以在后台切换 系统介绍: 1.网站后台采用主流的 SSM 框架 jsp JSTL,网站后台采用freemaker静态化模版引擎生成html 2.因为是生成的html,所以访问 ...

  9. ts中类的方法和抽象类

    // 类里面的修饰符 typescript提供三种修饰符 /* public: 公有 在类里面.类外面.子类都可以访问 (默认不加就是public) protected: 保护 在类里面和子类可以访问 ...

  10. 《HTML&CSS design and build websites》学习笔记(1)

    Chapter 1: Structure Chapter 2: Text <head>表示标题,默认显示在第一行. <title>表示抬头,显示在浏览器的标签页,还有窗口栏. ...