POJ1273【网络流】
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 91824 | Accepted: 35588 |
Description
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
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
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
Source
#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【网络流】的更多相关文章
- POJ1273 网络流-->最大流-->模板级别-->最大流常用算法总结
一般预流推进算法: 算法思想: 对容量网络G 的一个预流f,如果存在活跃顶点,则说明该预流不是可行流. 预流推进算法就是要选择活跃顶点,并通过它把一定的流量推进到它的邻接顶点,尽可能将正的赢余减少为0 ...
- 【生活没有希望】poj1273网络流大水题
你不能把数据规模改大点吗= =我优化都不加都过了 #include <cstdio> #define INF 2147483647 int n,m,ans,x,y,z,M; ],l[],f ...
- Drainage Ditches(POJ1273+网络流+Dinic+EK)
题目链接:poj.org/problem?id=1273 题目: 题意:求最大流. 思路:测板子题,分别用Dinic和EK实现(我的板子跑得时间均为0ms). Dinic代码实现如下: #includ ...
- poj1273 网络流入门题 dinic算法解决,可作模板使用
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62078 Accepted: 2384 ...
- 最大流算法-ISAP
引入 最大流算法分为两类,一种是增广路算法,一种是预留推进算法.增广路算法包括时间复杂度\(O(nm^2)\)的EK算法,上界为\(O(n^2m)\)的Dinic算法,以及一些其他的算法.EK算法直接 ...
- ACM/ICPC 之 网络流入门-EK算法(参考模板)(POJ1273)
基于残留网络与FF算法的改进-EK算法,核心是将一条边的单向残留容量的减少看做反向残留流量的增加. //网络流 //EK算法 //Time:16Ms Memory:348K #include<i ...
- 【网络流】POJ1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 78671 Accepted: 3068 ...
- [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)
题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...
- 网络流相关知识点以及题目//POJ1273 POJ 3436 POJ2112 POJ 1149
首先来认识一下网络流中最大流的问题 给定一个有向图G=(V,E),把图中的边看做成管道,边权看做成每根管道能通过的最大流量(容量),给定源点s和汇点t,在源点有一个水源,在汇点有一个蓄水池,问s-t的 ...
- POJ1273 USACO 4.2.1 Drainage Ditches CodeVS1993草地排水 网络流 最大流 SAP
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 传送门 - POJ 传送门 - CodeVS 题意概括 给出一个图,告诉你边和容量,起点是1,汇点是n,让你求最大流. 题解 ...
随机推荐
- toj 3761 Egg Problem (好题~~)
Egg Problem 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交: 22 测试通过: 7 描述 There is a very interest ...
- 初识matlab
1 matlab概貌 MATLAB是MATrix LABoratory(矩阵实验室)的缩写,是一款由美国The MathWorks公司出品的商业数学软件.matlab是一种用于算法开发.数据可视化.数 ...
- JavaScript之if流程控制演练,if写在区间内怎么解决
什么是编程?通俗意见上来讲,就是把人的思维与步骤通过代码的形式书写展示出来,JavaScript的流程控制包含条件判断if,switch选择,循环for while:if(表达式 条件)=>真{ ...
- mybatis中用到的9种设计模式
1.Builder模式:例如SqlSessionFactoryBuilder.XMLConfigBuilder.XMLMapperBuilder.XMLStatementBuilder.CacheBu ...
- 论文-MobileNet-V1、ShuffleNet-V1、MobileNet-V2、ShuffleNet-V2、MobileNet-V3
1.结构对比 1)MobileNet-V1 2)ShuffleNet-V1 3)MobileNet-V2 4)ShuffleNet-V2
- 英语语法 ( Spoken language )
- - - -------------- 1,五个语序: 主语+谓语(中英语序一致)主语+系动词+表语 (中英语序一致)主语+谓语+宾语(中英语序一致)主语+谓语+间宾+直宾(中英语序一致)主语+谓语 ...
- 在 Node 中使用 formidable 处理文件上传
具体使用方式参照官方文档:https://www.npmjs.com/package/formidable 第一:安装: # npm install --save formidable yarn ad ...
- thinkphp 5.0 后台数据修改
html代码 <table class="easyui-datagrid" data-options="singleSelect:true,collapsible: ...
- iptables (一) 主机防火墙和网络防火墙
Firewall : 防火墙,隔离工具:工作于主机或网络的边缘,对于进出本主机或网络的报文根据事先定义好的检测规则作匹配,对于能够被规则所匹配到的报文做出相应处理的组件:有主机防火墙和网络防火墙 Ip ...
- 【35】单层卷积网络(simple convolution)
今天我们要讲的是如何构建卷积神经网络的卷积层,下面来看个例子. 上节课,我们已经讲了如何通过两个过滤器卷积处理一个三维图像,并输出两个不同的4×4矩阵.假设使用第一个过滤器进行卷积,得到第一个4× ...