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. Web项目构建

    Gradle为Web开发提供了两个插件,war和jetty apply plugin: 'war' apply plugin: 'jetty' war插件继承了java插件,jetty插件继承了war ...

  2. git详细教程

    Table of Contents 1 Git详细教程 1.1 Git简介 1.1.1 Git是何方神圣? 1.1.2 重要的术语 1.1.3 索引 1.2 Git安装 1.3 Git配置 1.3.1 ...

  3. linux patch 格式与说明(收录)

    转:http://blog.chinaunix.net/uid-26813001-id-3282954.html 首先介绍一下diff和patch.在这里不会把man在线文档上所有的选项都介绍一下,那 ...

  4. WAC集中转发部署

    1,背景: sta属于vlan20.ap属于vlan20.本地转发. 现象: Ap获取到的地址是vlan20的地址池的某地址 用户无法获取地址. 注意:本地转发,ap获取地址dhcp包不走隧道.用户获 ...

  5. MySQL主从同步几个文件

    MySQL主从同步:   M锁表 M导出S导入 M解锁 M建同步帐号 S获取点位:产生master.info S开启同步   3306: mysql-bin.0000x mysql-bin.index ...

  6. C++ List的用法(整理)

    转自http://blog.csdn.net/lskyne/article/details/10418823 Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和 ...

  7. CentOS 7 添加win7启动项——修改默认启动项

    CentOS 7使用grub2引导启动,在win7之后装完CentOS再启动会丢失win7启动项. 首先,添加win7启动项,步骤如下: 1.使用root登陆系统 2.用文本编辑器打开 /boot/g ...

  8. Integrating JAD decompiler into JDeveloper(转)

    原文地址:Integrating JAD decompiler into JDeveloper In JDeveloper, when debugging or otherwise navigatin ...

  9. C#进阶系列——WebApi身份认证解决方案:Basic基础认证 (转)

    http://www.cnblogs.com/landeanfen/p/5287064.html 前言:最近,讨论到数据库安全的问题,于是就引出了WebApi服务没有加任何验证的问题.也就是说,任何人 ...

  10. chrome浏览器插件启动本地应用程序

    chrome浏览器插件启动本地应用程序 2014-04-20 00:04:30|  分类: 浏览器插件|举报|字号 订阅     下载LOFTER我的照片书  |     chrome的插件开发这里就 ...