Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13273    Accepted Submission(s): 6288

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
 
 
第一道最大流题目!
 
 #include <iostream>
#include <queue>
#include<string.h>
using namespace std;
#define arraysize 201
int maxData = 0x7fffffff;
int capacity[arraysize][arraysize]; //记录残留网络的容量
int flow[arraysize]; //标记从源点到当前节点实际还剩多少流量可用
int pre[arraysize]; //标记在这条路径上当前节点的前驱,同时标记该节点是否在队列中
int n,m;
queue<int> myqueue;
int BFS(int src,int des)
{
int i,j;
while(!myqueue.empty()) //队列清空
myqueue.pop();
for(i=;i<m+;++i)
{
pre[i]=-;
}
pre[src]=;
flow[src]= maxData;
myqueue.push(src);
while(!myqueue.empty())
{
int index = myqueue.front();
myqueue.pop();
if(index == des) //找到了增广路径
break;
for(i=;i<m+;++i)
{
if(i!=src && capacity[index][i]> && pre[i]==-)
{
pre[i] = index; //记录前驱
flow[i] = min(capacity[index][i],flow[index]); //关键:迭代的找到增量
myqueue.push(i);
}
}
}
if(pre[des]==-) //残留图中不再存在增广路径
return -;
else
return flow[des];
}
int maxFlow(int src,int des)
{
int increasement= ;
int sumflow = ;
while((increasement=BFS(src,des))!=-)
{
int k = des; //利用前驱寻找路径
while(k!=src)
{
int last = pre[k];
capacity[last][k] -= increasement; //改变正向边的容量
capacity[k][last] += increasement; //改变反向边的容量
k = last;
}
sumflow += increasement;
}
return sumflow;
}
int main()
{
int i,j;
int start,end,ci;
while(cin>>n>>m)
{
memset(capacity,,sizeof(capacity));
memset(flow,,sizeof(flow));
for(i=;i<n;++i)
{
cin>>start>>end>>ci;
if(start == end) //考虑起点终点相同的情况
continue;
capacity[start][end] +=ci; //此处注意可能出现多条同一起点终点的情况
}
cout<<maxFlow(,m)<<endl;
}
return ;
}
刘汝佳书上给的邻接表算法
 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
#include <string.h>
using namespace std;
const int MAX = ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
};
int n,m;
vector<Edge> edge;
vector<int> g[MAX];
int a[MAX],p[MAX];
void init()
{
for(int i = ; i <= m; i++)
g[i].clear();
edge.clear();
}
int Maxflow(int s,int t)
{
int flow = ;
while(true)
{
memset(a,,sizeof(a));
queue<int> q;
q.push(s);
a[s] = INF;
while(q.size())
{
int x = q.front();
q.pop();
int len = g[x].size();
for(int i = ; i < len; i++)
{
Edge e = edge[ g[x][i] ];
if(a[e.to] == && e.cap > e.flow)
{
p[e.to] = g[x][i];
a[e.to] = min(a[x],e.cap - e.flow);
q.push(e.to);
}
}
if(a[t])
break;
}
if(a[t] == )
break;
for(int i = t; i != s; i = edge[ p[i] ].from)
{
edge[ p[i] ].flow += a[t];
edge[ p[i] ^ ].flow -= a[t];
}
flow += a[t];
}
return flow;
}
int main()
{
while(scanf("%d%d", &n,&m) != EOF)
{
int s,e,v,t;
init();
for(int i = ; i <= n; i++)
{
scanf("%d%d%d",&s,&e,&v);
edge.push_back(Edge(s,e,v,));
edge.push_back(Edge(e,s,,));
t = edge.size();
g[s].push_back(t - );
g[e].push_back(t - );
} printf("%d\n",Maxflow(,m));
}
}

HD1532Drainage Ditches(最大流模板裸题 + 邻接表)的更多相关文章

  1. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  2. 三种邻接表存图模板:vector邻接表、数组邻接表、链式前向星

    vector邻接表: ; struct Edge{ int u,v,w; Edge(int _u=0,int _v=0,int _w=0){u=_u,v=_v,w=_w;} }; vector< ...

  3. 算法模板——sap网络最大流 3(递归+邻接表)

    实现功能:同前 程序还是一如既往的优美,虽然比起邻接矩阵的稍稍长了那么些,不过没关系这是必然,但更重要的一个必然是——速度将是一个质的飞跃^_^(这里面的point指针稍作了些创新——anti指针,这 ...

  4. POJ 1273 Drainage Ditches | 最大流模板

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  5. 邻接表(C++)

    adj_list_network_edge.h // 邻接表网边数据类模板 template <class WeightType> class AdjListNetworkEdge { p ...

  6. hdu 1532 Drainage Ditches(最大流模板题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)

    最大流模板题 大部分Edmond-Karp算法代码都是邻接矩阵实现,试着改成了邻接表. #include <iostream> #include <cstdio> #inclu ...

  8. hdu Flow Problem (最大流 裸题)

    最大流裸题,贴下模版 view code#include <iostream> #include <cstdio> #include <cstring> #incl ...

  9. [hdu3549]Flow Problem(最大流模板题)

    解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...

随机推荐

  1. jquery中的get和set

    jquery中通过参数的个数来判断是get方法还是set方法: css: function(name, value ) { return value !== undefined ? jQuery.st ...

  2. android 合并两个jar包

    你所要导出的类里边用到了别的jar包.比如说你写的类连接了数据库,用到数据库驱动包oracl.jar(也就是你导入到Myeclipse或eclipse的jdbc包).. .在dos环境下,进入到D盘的 ...

  3. android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留几个类)

    在使用android-async-http的时候我的apl 更新到了23,我的build version也是23的时候出现了,org.apache.http.Header这个类找不到的情况,原因是在a ...

  4. Nutch搜索引擎(第2期)_ Solr简介及安装

    1.Solr简介 Solr是一个高性能,采用Java5开发,基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展并对查询性能进行了优化 ...

  5. js字符串截取函数slice()、substring()、substr()

    摘要 在js中字符截取函数有常用的三个slice().substring().substr()了,下面我来给大家介绍slice().substring().substr()函数在字符截取时的一些用法与 ...

  6. MJExtension的使用:https://github.com/CoderMJLee/MJExtension

    MJExtension能做什么? MJExtension是一套字典和模型之间互相转换的超轻量级框架 MJExtension能完成的功能 字典(JSON) --> 模型(Model) 模型(Mod ...

  7. 对于AP中为什么有4个WEP KEY的分析

    这篇文章简要分析一下为什么有4个WEP KEY,及其中的一些原因. SPEC 用过AP的都知道,AP中有4个WEP KEY,但是为什么要设置4个呢,这个是WEP帧的格式决定的: 图中的keyid是2个 ...

  8. 关于一个每天请求50W次接口的设计实现过程

    从大学开始关注博客园,到工作之后注册了博客园账号,直至今日终于能够静下心来将自己个人的所学,所得,所悟能够分享出来与大家分享,好开心~ 言归正传,需求背景是博主所在的公司为一个在线OTA公司,客户端上 ...

  9. 解决angular2页面刷新后报404错误

    如果你的angular项目部署到一个tomcat容器里面,localhost:8080是JavaWeb的主页,localhost:8080/driver/login是你angular2项目的登陆地址. ...

  10. Orchard 刨析:Caching

    关于Orchard中的Caching组件已经有一些文章做了介绍,为了系列的完整性会再次对Caching组件进行一次介绍. 缓存的使用 在Orchard看到如下一段代码: 可以看到使用缓存的方法Get而 ...