HDU1532 Drainage Ditches SAP+链式前向星
Drainage Ditches
Total Submission(s): 17065 Accepted Submission(s): 8066
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.
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.
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
const int maxm=maxn*maxn;
struct edgenode {
int c,next,to;
}edges[maxm];
int head[maxn];//链式前向星
int index;
int sapmaxflow(int source, int sink, int n) {
//numh:用于GAP优化的统计高度数量数组;h:距离标号数组;curedges:当前弧数组;pre:前驱数组
int numh[maxn],h[maxn],curedges[maxn],pre[maxn];
//初始化最大流为0
int cur_flow,flow_ans=,u,tmp,neck,i;
memset(h,,sizeof(h));
memset(numh,,sizeof(numh));
memset(pre,-,sizeof(pre));
//初始化当前弧为第一条邻接边
for(i=;i<=n;++i) curedges[i]=head[i];
numh[]=n;
u=source;
//当h[start]>=n时,网络中能够肯定出现了GAP
while(h[source]<n) {
if(u==sink) {
cur_flow=INF;
//增广成功,寻找“瓶颈”边
for(i=source;i!=sink;i=edges[curedges[i]].to) {
if(cur_flow>edges[curedges[i]].c) {
neck=i;
cur_flow=edges[curedges[i]].c;
}
}
//修改路径上的容量
for(i=source;i!=sink;i=edges[curedges[i]].to) {
tmp=curedges[i];
edges[tmp].c-=cur_flow;
edges[tmp^].c+=cur_flow;
}
flow_ans+=cur_flow;
//下次增广从瓶颈边开始
u=neck;
}
for(i=curedges[u];i!=-;i=edges[i].next) if(edges[i].c&&h[u]==h[edges[i].to]+) break;
if(i!=-) {
curedges[u]=i;
pre[edges[i].to]=u;
u=edges[i].to;
} else {
//GAP优化
if(==--numh[h[u]]) break;
curedges[u]=head[u];
for(tmp=n,i=head[u];i!=-;i=edges[i].next) if(edges[i].c) tmp=min(tmp,h[edges[i].to]);
//重新标号并且从当前点前驱重新增广
h[u]=tmp+;
++numh[h[u]];
if(u!=source) u=pre[u];
}
}
return flow_ans;
}
void addedge(int u, int v, int c) {
edges[index].to=v;
edges[index].c=c;
edges[index].next=head[u];
head[u]=index++;
edges[index].to=u;
edges[index].c=;
edges[index].next=head[v];
head[v]=index++;
}
int main() {
int n,m;
while(~scanf("%d%d",&m,&n)) {
int u,v,c;
for(int i=;i<maxm;++i) edges[i].next=-;
for(int i=;i<maxn;++i) head[i]=-;
index=;
for(int i=;i<m;++i) {
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,c);
}
printf("%d\n",sapmaxflow(,n,n));
}
return ;
}
HDU1532 Drainage Ditches SAP+链式前向星的更多相关文章
- zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)
2131: Can Win Time Limit: 1 Sec Memory Limit: 128 MB Submit: 431 Solved: 50 SubmitStatusWeb Board ...
- poj-1459-最大流dinic+链式前向星-isap+bfs+stack
title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...
- 链式前向星+SPFA
今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...
- 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板
一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...
- hdu2647 逆拓扑,链式前向星。
pid=2647">原文地址 题目分析 题意 老板发工资,可是要保证发的工资数满足每一个人的期望,比方A期望工资大于B,仅仅需比B多1元钱就可以.老板发的最低工资为888元.输出老板最 ...
- 图的存储结构:邻接矩阵(邻接表)&链式前向星
[概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...
- 【模板】链式前向星+spfa
洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...
- zzuli 2130: hipercijevi 链式前向星+BFS+输入输出外挂
2130: hipercijevi Time Limit: 1 Sec Memory Limit: 128 MB Submit: 595 Solved: 112 SubmitStatusWeb B ...
- UESTC30-最短路-Floyd最短路、spfa+链式前向星建图
最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...
随机推荐
- tarjan求强连通分量+缩点+割点以及一些证明
“tarjan陪伴强联通分量 生成树完成后思路才闪光 欧拉跑过的七桥古塘 让你 心驰神往”----<膜你抄> 自从听完这首歌,我就对tarjan开始心驰神往了,不过由于之前水平不足,一 ...
- IdentityServer4 实现自定义 GrantType 授权模式
OAuth 2.0 默认四种授权模式(GrantType): 授权码模式(authorization_code) 简化模式(implicit) 密码模式(password) 客户端模式(client_ ...
- 使用Identity Server 4建立Authorization Server (3)
预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 第一部分: http://www.cnblogs.com/cgzl/p/7780559.html 第二 ...
- ionic3.0--angular4.0 引入第三方插件库的方法
ionic3.0 引入第三方插件 (swiper),方法很多,现详细说明下官方推荐(typings)做法. 1.全局安装Typings 1. npm install -g typings 2.搜索你 ...
- ASP.NET Core中的OWASP Top 10 十大风险-失效的访问控制与Session管理
不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: https://dotnetcoretutorials.com/201 ...
- linux-head
linux-head 用来查看文件的内容的命令 命令参数 -n num:显示指定文件的前num行 -c num:显示指定文件的前num个字符 命令:head b.txt : 如果不加参数就默认 ...
- 2000W条数据,加入全文检索的总结
一) 前期准备测试: 旧版的MySQL的全文索引只能用在MyISAM表格的char.varchar和text的字段上. 不过新版的MySQL5.6.24上InnoDB引擎也加入了全文索引,所以具体信息 ...
- 高阶函数,柯里化,sort排序
高阶函数概念 first class object: 函数在python中时一等公民. 函数也是对象,可调用的对象. 函数可以作为普通变量,参数,返回值等等. 高阶函数: ...
- postgis几何操作函数集
管理操作函数 AddGeometryColumn - Adds a geometry column to an existing table of attributes. By default use ...
- app.config 配置多项 配置集合 自定义配置(3)
再说说利用app.config配置多个自定义的方法.先看这个例子:美国家庭Simpson的家里有父亲母亲和三个儿女,而中国的老王只有独生子女.结构如下: <?xml version=" ...