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) 在每年的校赛里,所有进入决赛的同 ...
随机推荐
- 温故而知新 Volley源码解读与思考
相比新的网络请求框架Volley真的很落后,一无是处吗,要知道Volley是由google官方推出的,虽然推出的时间很久了,但是其中依然有值得学习的地方. 从命名我们就能看出一些端倪,volley中 ...
- 写给想成为前端工程师的同学们 ―前端工程师是做什么的?a
前端工程师是做什么的? 前端工程师是互联网时代软件产品研发中不可缺少的一种专业研发角色.从狭义上讲,前端工程师使用 HTML.CSS.JavaScript 等专业技能和工具将产品UI设计稿实现成网站产 ...
- angular之scope详解
AngularJS的一些指令会创建子作用域,而子作用域会继承自父作用域,大致可分为以下3种 1.创建子作用域并继承父作用域的指令 ng-repeat ng-include ng-switch ng-c ...
- spark在yarn-cluster模式,错误查找方法
yarn logs -applicationId application_xxxx_xxx 可选(">exception")
- poj2689Prime Distance(大区间筛素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19635 Accepted: 5273 D ...
- SQL表连接查询(inner join(join)、full join、left join、right join、cross join)
下面列出了您可以使用的 JOIN 类型,以及它们之间的差异. JOIN: 如果表中有至少一个匹配,则返回行(join=inner join) LEFT JOIN: 即使右表中没有匹配,也从左表返回所有 ...
- HTML学习笔记 css定位(静态,相对,固定,绝对布局)偏移案例 第十二节 (原创) 参考使用表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 前端基于react,后端基于.net core2.0的开发之路(1) 介绍
文章提纲目录 1.前端基于react,后端基于.net core2.0的开发之路(1) 介绍 2.前端基于react,后端基于.net core2.0的开发之路(2) 开发环境的配置,注意事项,后端数 ...
- word的标题行前面数字变成黑框 解决方案
如图 图1如下 图2如下 图3如下 如下解决 1. Put your cursor on the heading just right of the black box.将光标定位到标题中,紧邻黑框的 ...
- 记录一次Session偶尔获取不到的解决过程
导读 平台下某子系统有密码登录需求,初步考虑用Session,登录后设置Session[key]=value;Session中若某key对应的Session,即Session[key]为null则限制 ...