A - Drainage Ditches

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

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
 
 
题意: 给一个有向有环图,给出每条边到容量上限,无下限,源点是1,汇点是n,求最大流。赤裸裸点网络流,我用的ISAP算法。第一次过点网络流^_^
 
思路: ISAP模板过。白书上没给ISAP的BFS。。搞了好久才知道怎么改。。
 
代码
 
#include <vector>
#include <cstdio>
#include <cstring>
#include <queue>
#define FOR(i,n) for(i=1;i<=(n);i++)
using namespace std;
const int INF = 2e9+;
const int N = ; struct Edge{
int from,to,cap,flow;
}; struct ISAP{
int n,m,s,t;
int p[N],num[N];
vector<Edge> edges;
vector<int> G[N];
bool vis[N];
int d[N],cur[N];
void init(int _n,int _m)
{
n=_n; m=_m;
int i;
edges.clear();
FOR(i,n)
{
G[i].clear();
d[i]=INF;
}
}
void AddEdge(int from,int to,int cap)
{
edges.push_back((Edge){from,to,cap,});
edges.push_back((Edge){to,from,,});
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool BFS()
{
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(t);
d[t]=;
vis[t]=;
while(!Q.empty())
{
int x = Q.front(); Q.pop();
for(unsigned i=;i<G[x].size();i++)
{
Edge& e = edges[G[x][i]^];
if(!vis[e.from] && e.cap>e.flow)
{
vis[e.from]=;
d[e.from] = d[x]+;
Q.push(e.from);
}
}
}
return vis[s];
}
int Augment()
{
int x=t, a=INF;
while(x!=s)
{
Edge& e = edges[p[x]];
a = min(a,e.cap-e.flow);
x = edges[p[x]].from;
}
x = t;
while(x!=s)
{
edges[p[x]].flow+=a;
edges[p[x]^].flow-=a;
x=edges[p[x]].from;
}
return a;
}
int Maxflow(int _s,int _t)
{
s=_s; t=_t;
int flow = , i;
BFS();
// FOR(i,n) printf("%d ",d[i]); puts("");
if(d[s]>=n) return ;
memset(num,,sizeof(num));
memset(p,,sizeof(p));
FOR(i,n) if(d[i]<INF) num[d[i]]++;
int x=s;
memset(cur,,sizeof(cur));
while(d[s]<n)
{
if(x==t)
{
flow+=Augment();
x=s;
}
int ok=;
for(unsigned i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(e.cap>e.flow && d[x]==d[e.to]+)
{
ok=;
p[e.to]=G[x][i];
cur[x]=i;
x=e.to;
break;
}
}
if(!ok)
{
int m=n-;
for(unsigned i=;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(e.cap>e.flow) m=min(m,d[e.to]);
}
if(--num[d[x]]==) break;
num[d[x]=m+]++;
cur[x]=;
if(x!=s) x=edges[p[x]].from;
}
}
return flow;
}
}; ISAP isap; int main()
{
freopen("in","r",stdin);
int n,m,u,v,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
isap.init(n,m);
while(m--)
{
scanf("%d%d%d",&u,&v,&c);
isap.AddEdge(u,v,c);
//isap.AddEdge(v,u,c);
}
printf("%d\n",isap.Maxflow(,n));
}
return ;
}

ISAP 模板

注意用宏定义的FOR来做点的初始化,有些题目点所从0开始编号有些所从1开始,所以需要用一个宏定义

struct Edge{
int from,to,cap,flow;
}; struct ISAP{
int n,m,s,t;
int p[N],num[N];
vector<Edge> edges;
vector<int> G[N];
bool vis[N];
int d[N],cur[N];
void init(int _n,int _m)
{
n=_n; m=_m;
int i;
edges.clear();
FOR(i,n)
{
G[i].clear();
d[i]=INF;
}
}
void AddEdge(int from,int to,int cap)
{
edges.push_back((Edge){from,to,cap,});
edges.push_back((Edge){to,from,,});
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool BFS()
{
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(t);
d[t]=;
vis[t]=;
while(!Q.empty())
{
int x = Q.front(); Q.pop();
for(unsigned i=;i<G[x].size();i++)
{
Edge& e = edges[G[x][i]^];
if(!vis[e.from] && e.cap>e.flow)
{
vis[e.from]=;
d[e.from] = d[x]+;
Q.push(e.from);
}
}
}
return vis[s];
}
int Augment()
{
int x=t, a=INF;
while(x!=s)
{
Edge& e = edges[p[x]];
a = min(a,e.cap-e.flow);
x = edges[p[x]].from;
}
x = t;
while(x!=s)
{
edges[p[x]].flow+=a;
edges[p[x]^].flow-=a;
x=edges[p[x]].from;
}
return a;
}
int Maxflow(int _s,int _t)
{
s=_s; t=_t;
int flow = , i;
BFS();
if(d[s]>=n) return ;
memset(num,,sizeof(num));
memset(p,,sizeof(p));
FOR(i,n) num[d[i]]++;
int x=s;
memset(cur,,sizeof(cur));
while(d[s]<n)
{
if(x==t)
{
flow+=Augment();
x=s;
}
int ok=;
for(unsigned i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(e.cap>e.flow && d[x]==d[e.to]+)
{
ok=;
p[e.to]=G[x][i];
cur[x]=i;
x=e.to;
break;
}
}
if(!ok)
{
int m=n-;
for(unsigned i=;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(e.cap>e.flow) m=min(m,d[e.to]);
}
if(--num[d[x]]==) break;
num[d[x]=m+]++;
cur[x]=;
if(x!=s) x=edges[p[x]].from;
}
}
return flow;
}
};
 
 
 
 

HDU 1532 Drainage Ditches (网络流)的更多相关文章

  1. hdu 1532 Drainage Ditches(网络流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意是:农夫约翰要把多个小池塘的水通过池塘间连接的水渠排出去,从池塘1到池塘M最多可以排多少 ...

  2. HDU 1532 Drainage Ditches(网络流模板题)

    题目大意:就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速, 本题就是让你求出最大流速,无疑要运用到求最大流了.题中m为水沟数, ...

  3. HDU 1532 Drainage Ditches (最大网络流)

    Drainage Ditches Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) To ...

  4. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

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

  5. HDU 1532 Drainage Ditches(最大流 EK算法)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...

  6. POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

    Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...

  7. poj 1273 && hdu 1532 Drainage Ditches (网络最大流)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53640   Accepted: 2044 ...

  8. hdu 1532 Drainage Ditches(最大流)

                                                                                            Drainage Dit ...

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

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

随机推荐

  1. linux 设置静态IP方法

    本系统使用 linux redhat 7.2 1.   修改ip vi   /etc/sysconfig/network-scripts/ifcfg-eno16777736 2. 修改数据项如下 3. ...

  2. EasyNVR H5流媒体服务器方案架构设计之视频能力平台

    历经过程 阶段一:经历过传统安防开发过程的开发者都有一种感觉,就是各种业务交织,各个模块的开发扯皮,各种数据库连接冲突,这很让开发工作效率很低,而且会给整体的开发带来负面影响,更重要的是,耦合度太高, ...

  3. sed命令使用举例

    选择操作的行范围 sed -n '1,2p' testsed2.txt  匹配第1到2行 sed -n '/a/,/b/p' testsed2.txt  匹配从包含a的行到包含b的行 sed -n ' ...

  4. Netty聊天室-源码

    目录 Netty聊天室 源码工程 写在前面 [百万级流量 聊天室实战]: [分布式 聊天室] [Spring +Netty]: [Netty 原理] 死磕 系列 [提升篇]: [内力大增篇]: 疯狂创 ...

  5. 题解 CF576C 【Points on Plane】

    题解 CF576C [Points on Plane] 一道很好的思维题. 传送门 我们看这个曼哈顿距离,显然如果有一边是按顺序排列的,显然是最优的,那另一边怎么办呢? 假如你正在\(ioi\)赛场上 ...

  6. flex做页面。用来做视频的后台服务器是fms

    作为新一代的富客户端互联网技术的佼佼者,Flex这种技术已经被越来越多的公司所采用,被越来越多的用户和程序员所接受.以下列出Flex十大优势: 1.Flex与Flash:可以让普通程序员开发制作Fla ...

  7. 运用<ul><li>做导航栏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. git显示颜色配置

    启用默认的颜色设置可以使用如下命令 git config --global color.ui true 如果不喜欢花花绿绿的颜色,可以将其关闭: git config --global color.u ...

  9. sqrt源码

    先找出接近m的浮点数,然后通过下面的不等式中的等于条件得到其平方根. #include <iostream> #include <math.h> using namespace ...

  10. DubboAdmin平台

    DubboAdmin部署 将dubbo-admin.war放入到TomcatWebapps目录下,修改dubbo.properties中的Zookeeper连接地址即可. dubbo-admin放到 ...