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) 在每年的校赛里,所有进入决赛的同 ...
随机推荐
- Python BDD自动化测试框架初探
1. 什么是BDD BDD全称Behavior Driven Development,译作"行为驱动开发",是基于TDD (Test Driven Development 测试驱动 ...
- spring mvc+mybatis+maven集成tkmapper+pagehelper
<!-- maven tkmapper引入--> <dependency> <groupId>tk.mybatis</groupId> <arti ...
- javascript高性能写法
看到一篇不错的博文,如果想写出比较高性能的代码,可参看这个链接http://developer.51cto.com/art/200906/131335.htm
- git使用教程之git基础
1 获取Git仓库 在现有目录中初始化仓库 git init 克隆现有的仓库 git clone https://github.com/yangwang12345/node_test.git Git ...
- 获取 修改 CSS 样式
内联(style里的)样式 element.style.color element.style.getPropertyValue("color") 非内联样式 window.g ...
- 关于jQuery.click()函数
最近接手了前同事的项目,关于使用线程控制实现代码热插拔功能! 在线程中,使用了ChatWebSocketHandler,与前台进行实时交互,今天我拿到需求是这样的,结合chatWebSocketHan ...
- ⒀bootstrap组件 选项卡 基础案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- 小米Java程序员第二轮面试10个问题,你是否会被刷掉?
近日,开发者头条上分享了一篇"小米java第二轮面经",有很多的java程序员表示非常有兴趣. 下面l就和各位分享小米java第二轮面经(华为java工程师笔试面试题可以看文章某尾 ...
- winform音频播放器(有声小说[凡人修仙传])
该程序采用多线程的技术及DataGridView单元格扩展的技术 1.获取下载列表 private void GetDownList() { //System.Web.HttpUtility.UrlD ...
- C# Ioc容器Unity,简单实用
开头先吐槽一下博客园超级不好用,添加图片后就写不动字了,难道是bug 好进入正题,先来说下依赖注入,简单来说就是定义好接口,上层代码调用接口,具体实现通过配置文件方式去指定具体实现类. 首先我们需要通 ...