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. 静态SRAM芯片工作原理

    下面谈谈当存储字节的过程是怎样的:下面的示意图显示的也仅仅是最简单状态下的情况,当内存条上仅剩一个RAM芯片的情况.对于X86处理器,它通过地址总线发出一个具有22位二进制数字的地址编码--其中11位 ...

  2. Linux nohup不输出日志文件的方法

    引用:https://blog.csdn.net/guotao15285007494/article/details/84136234 最近在Linux上部署视频流推送应用时,由于网络不稳定等原因程序 ...

  3. 【DTOJ】2704:数字互换

    DTOJ 2704:数字互换  解题报告 2017.11.11 第一版 ——由翱翔的逗比w原创 题目信息: 题目描述 输入两个数作为交换数,输出已交换顺序后的两个值. 输入 两个整数,空格隔开 输出 ...

  4. RocketMQ幂等性问题

    什么是幂等性: 在编程中一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同. 当出现消费者对某条消息重复消费的情况时,重复消费的结果与消费一次的结果是相同的,并且多次消费并未对业务系 ...

  5. 20200228 尚硅谷-NIO

    尚硅谷-NIO Java NIO简介 Java NIO(New IO.Non Blocking IO)是从Java1.4版本开始引入的新的 IO API,可以替代标准的 Java IO API. NI ...

  6. java Spring boot Docker打包

    https://blog.csdn.net/Stephanie_1/article/details/88831993

  7. Python的基本语法和数据类型(简明教程)

    声明:借鉴Python 简明教程 一.注释 注释: 就是对代码的解释 方便大家阅读python代码,在编辑器中快捷键:  notepad :ctrl + q    pycharm: ctrl + / ...

  8. Pikachu-RCE(远程命令/代码执行漏洞)

    RCE(remote command/code execute)概述 RCE漏洞,可以让攻击者直接向后台服务器远程注入操作系统命令或者代码,从而控制后台系统. 远程系统命令执行一般出现这种漏洞,是因为 ...

  9. JS 数组常见操作汇总,数组去重、降维、排序、多数组合并实现思路整理

    壹 ❀ 引 JavaScript开发中数组加工极为常见,其次在面试中被问及的概率也特别高,一直想整理一篇关于数组常见操作的文章,本文也算了却心愿了. 说在前面,文中的实现并非最佳,实现虽然有很多种,但 ...

  10. RS323串口连接仪器,接收仪器信息

    SerialPort sp1 = new SerialPort(); getBloodPressur(); public void getBloodPressur() { try { string[] ...