Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17065    Accepted Submission(s): 8066
Problem Description
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage
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.
 
Input
The input includes several cases. 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
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
 
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
 
Sample Output
50
 思路:裸SAP,注意建边的过程。三种优化请忽略,验证模板
代码:
 #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+链式前向星的更多相关文章

  1. zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)

    2131: Can Win Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 431  Solved: 50 SubmitStatusWeb Board ...

  2. poj-1459-最大流dinic+链式前向星-isap+bfs+stack

    title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...

  3. 链式前向星+SPFA

    今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...

  4. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  5. hdu2647 逆拓扑,链式前向星。

    pid=2647">原文地址 题目分析 题意 老板发工资,可是要保证发的工资数满足每一个人的期望,比方A期望工资大于B,仅仅需比B多1元钱就可以.老板发的最低工资为888元.输出老板最 ...

  6. 图的存储结构:邻接矩阵(邻接表)&链式前向星

    [概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...

  7. 【模板】链式前向星+spfa

    洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...

  8. zzuli 2130: hipercijevi 链式前向星+BFS+输入输出外挂

    2130: hipercijevi Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 595  Solved: 112 SubmitStatusWeb B ...

  9. UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

    最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...

随机推荐

  1. Java基础笔记1

    java (开源,跨操作系统)j2ee jre java基础 javaoop java高级 JDK(JAVA developer Kitool): java开发工具 (开发人员使用) JRE(java ...

  2. hadoop2集群中关键配置文件的记录

    配置HDFS  高可用 1.配置HDFS配置文件 $ vi hdfs-site.xml #写入 <configuration> #配置NameService 名字随便起 <prope ...

  3. LINUX环境下SVN安装与配置(利用钩子同步开发环境与测试环境)

    安装采用YUM一键安装: 1.环境Centos 6.6 2.安装svnyum -y install subversion 3.配置 建立版本库目录mkdir /www/svndata svnserve ...

  4. Turn the corner

    Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...

  5. CQRS微服务架构模式

    ​什么是微服务? 这是维基百科里面的定义:“微服务是面向服务架构(SOA)架构风格的一种变体,它将应用程序构建为一系列松散耦合的服务.在微服务体系结构中,服务应该是细粒度的,协议应该是轻量级的.将应用 ...

  6. ajax请求发送和页面跳转的冲突

    http://blog.csdn.net/allenliu6/article/details/77341538?locationNum=7&fps=1 在A页面中,有一个click事件,点击时 ...

  7. 简易RPC框架-代理

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  8. 不想再被鄙视?那就看进来! 一文搞懂Python2字符编码

    程序员都自视清高,觉得自己是创造者,经常鄙视不太懂技术的产品或者QA.可悲的是,程序员之间也相互鄙视,程序员的鄙视链流传甚广,作为一个Python程序员,自然最关心的是下面这幅图啦 我们项目组一值使用 ...

  9. 数据库连接(Oracle/sqlServer增、删、改、查)

    一.把数据库包导入项目中,并且Build path(oracle例如ojdbc6.jar;sqlServer例如sqljdbc.jar) 二.开始操作 1.普通连接: String className ...

  10. Robotium 框架学习之Class By

    Class By定义了页面元素的定位和支持哪些页面元素(至少我是这么理解的),使用及其简单:Used in conjunction with the web methods. Examples are ...