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) 在每年的校赛里,所有进入决赛的同 ...
随机推荐
- win10 安装Node.js 报错:2503
解决方法: 使用管理员打开CMD
- Turn the corner
Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...
- eclipse中删除多余的tomcat server
在eclipse菜单中选择Window--Show View--Server--Servers,打开这个服务窗口,将多余的服务删除即可. 如果每次启动中太卡没反应,那就是服务没选择好,或是端口冲突的原 ...
- addEventListener和attachEvent二者绑定的执行函数中的this不相同【转载】
yuanwen http://www.jb51.net/article/32511.htm 写 addEventListener 和 attachEvent 区别的博文不少,不过大部分都把重点放置于前 ...
- setImmediate()
在循环事件任务完成后马上运行指定代码 以前使用 setTimeout(fn, 0); Since browsers clamp their timers to 4ms, it really d ...
- Easy UI下拉列表默认选中(多行)与为文本框赋值
1.为单行文本框赋值 var data2 = $('#LoadArea').combobox("getData"); if (data2) { $('#id).combobox(' ...
- Flex布局(引用阮一峰大神)
Flex 布局教程:语法篇 http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html Flex 布局教程:实例篇 http://www.ruan ...
- Nginx-动态路由升级版
前几篇文章我们介绍了Nginx的配置.OpenResty安装配置.基于Redis的动态路由以及Nginx的监控. Nginx-OpenResty安装配置 Nginx配置详解 Nginx技术研究系列1- ...
- 【深度学习笔记】(一)TensorFlow安装及环境搭建
在学习了一段时间台大李宏毅关于deep learning的课程,以及一些其他机器学习的书之后,终于打算开始动手进行一些实践了. 感觉保完研之后散养状态下,学习效率太低了,于是便想白天学习,晚上对白天学 ...
- Intellij16创建Spring-Mybatis项目创(填)建(坑)记录,解决IDEA下找不到xml文件的问题
转入Intellij已经有1个月了,编程效率确实比Eclipse快了很多,而且可以直接使用Maven,然后就想写个小项目玩玩,架构搭建完后,想着万事俱备,又不是第一次玩框架,照葫芦画瓢撑死半天就能完成 ...