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. Python字符串字母大小写变换

    说明: 字符串就是一系列字符,在Python中用引号括起来的都是字符串,引号可以是单引号,也可以是双引号,比如:“This is a book.”  ‘This is an apple.’ 接下来简单 ...

  2. 在React中使用WebUploader实现大文件分片上传的踩坑日记!

    前段时间公司项目有个大文件分片上传的需求,项目是用React写的,大文件分片上传这个功能使用了WebUploader这个组件. 具体交互是: 1. 点击上传文件button后出现弹窗,弹窗内有选择文件 ...

  3. Re:连点器

    连点器 介绍 顾名思义,可以连续点的机器. 当然,连续可快可慢:机器意味着不许要人工点击:可以是生活中的机器,也可以是电脑中的程序. 现在,连点器网上一搜一大堆,什么鼠标连点精灵,鼠大侠……不仅有电脑 ...

  4. 安装Matlab R2017a 出现 “弹出DVD1 并插入DVD2” 解决办法超简单

    打开此电脑 找到驱动器虚拟镜像 右击选择弹出 点击另一个文件装载 点击确定即可

  5. export和export default的区别

    export和export default的区别一.export的使用1.直接输出export let words = ‘hello world!!!’export function output() ...

  6. 思科路由器、交换机配置Console 线线序 (亲测可以)

    网上有许多标准console线配置线序,在配置思科网络设备时都是不能用的,因为思科的console线序是专用的, 如下 水晶头侧 线序 B 白橙,橙,白绿,蓝 ,白蓝,绿,白粽,棕 对应串口侧线序 1 ...

  7. SpringCloud入门学习

    我相信,如果小伙伴们能来到这里,肯定对微服务有一定的认识. 我们之前创建web项目的时候,常见的有两种方式: 1).创建一个war包,然后放在servlet容器中运行(比如Tomcat等); 2).使 ...

  8. 【01】HTML_day01_02-认识HTML

    typora-copy-images-to: media 第01阶段.前端基础.认识HTML 学习目标 理解 HTML的概念 HTML标签的分类 HTML标签的关系 HTML标签的语义化 应用 HTM ...

  9. MySQL必知存储引擎

    Mysql存储引擎 1.MyISAM MySQL 5.0 之前的默认数据库引擎,最为常用.拥有较高的插入,查询速度,但不支持事务. 2.InnoDB事务型数据库的首选引擎,支持ACID事务,支持行级锁 ...

  10. 安装PHP到CentOS(YUM)

    运行环境 系统版本:CentOS Linux release 7.3.1611 软件版本:PHP-7.2 硬件要求:无 安装过程 1.配置YUM源 [root@localhost ~]# rpm -i ...