Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 68414   Accepted: 26487

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

Source

网络流最大流经典入门题,学习了算法竞赛入门经典的程序,我用两种方法解,即Dinic算法和ISAP算法。

Dinic算法(邻接表、无cur优化)

15705294

  ksq2013 1273 Accepted 732K 0MS G++ 1634B 2016-07-11 20:44:19
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m,s,t,nxt[800],first[800],ecnt;
struct Edge{int u,v,cap,flow;}e[800];
bool vis[800];
int d[800],cur[800];
int bfs()
{
memset(vis,false,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=0;
vis[s]=true;
while(!q.empty()){
int now=q.front();q.pop();
for(int i=first[now];i;i=nxt[i]){
if(!vis[e[i].v]&&e[i].cap>e[i].flow){
vis[e[i].v]=true;
d[e[i].v]=d[now]+1;
q.push(e[i].v);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t||a==0)return a;
int flow=0,f;
for(int i=first[x];i;i=nxt[i])
if(d[e[i].v]==d[x]+1&&(f=dfs(e[i].v,min(a,e[i].cap-e[i].flow)))>0){
e[i].flow+=f;
e[i^1].flow-=f;
flow+=f;
a-=f;
if(a==0)break;
}
return flow;
}
int Dinic()
{
int flow=0;
while(bfs()){
memset(cur,0,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
void Link()
{
memset(nxt,0,sizeof(nxt));
memset(first,0,sizeof(first));
for(int a,b,c;m;m--){
scanf("%d%d%d",&a,&b,&c);
e[++ecnt].u=a,e[ecnt].v=b,e[ecnt].cap=c,e[ecnt].flow=0;
nxt[ecnt]=first[a],first[a]=ecnt;
e[++ecnt].u=b,e[ecnt].v=a,e[ecnt].cap=0,e[ecnt].flow=0;
nxt[ecnt]=first[b],first[b]=ecnt;
}
}
int main()
{
while(~scanf("%d%d",&m,&n)){
s=1,t=n,ecnt=1;
memset(d,0,sizeof(d));
Link();
printf("%d\n",Dinic());
}
return 0;
}

ISAP算法(邻接表,有gap等优化)

15708393

  ksq2013 1273 Accepted 736K 16MS G++ 2300B 2016-07-12 10:59:51
#include<cstdio>
#include<cstring>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
int n,m,s,t,ecnt,first[800],nxt[800];
struct Edge{int u,v,cap,flow;}e[800];
bool vis[800];
int q[800],d[800],p[800],num[800],cur[800];
void Link()
{
memset(first,0,sizeof(first));
memset(nxt,0,sizeof(nxt));
for(int a,b,c;m;m--){
scanf("%d%d%d",&a,&b,&c);
e[++ecnt].u=a,e[ecnt].v=b,e[ecnt].cap=c,e[ecnt].flow=0;
nxt[ecnt]=first[e[ecnt].u];first[e[ecnt].u]=ecnt;
e[++ecnt].u=b,e[ecnt].v=a,e[ecnt].cap=0,e[ecnt].flow=0;
nxt[ecnt]=first[e[ecnt].u];first[e[ecnt].u]=ecnt;
}
}
void bfs()
{
memset(vis,false,sizeof(vis));
int head=0,tail=1;
q[0]=t;
d[t]=0;
vis[t]=true;
while(head^tail){
int now=q[head];head++;
for(int i=first[now];i;i=nxt[i])
if(!vis[e[i].u]&&e[i].cap>e[i].flow){
vis[e[i].u]=true;
d[e[i].u]=d[now]+1;
q[tail++]=e[i].u;
}
}
}
int Agument()
{
int x=t,a=INF;
while(x^s){
a=min(a,e[p[x]].cap-e[p[x]].flow);
x=e[p[x]].u;
}
x=t;
while(x^s){
e[p[x]].flow+=a;
e[p[x]^1].flow-=a;
x=e[p[x]].u;
}
return a;
}
int ISAP()
{
int flow=0;
bfs();
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)num[d[i]]++;
int x=s;
for(int i=1;i<=n;i++)cur[i]=first[i];//memset(cur,0,sizeof(cur));
while(d[s]<n){
if(!(x^t)){
flow+=Agument();
x=s;
}
bool advanced=false;
for(int i=cur[x];i;i=nxt[i])
if(e[i].cap>e[i].flow&&d[x]==d[e[i].v]+1){
advanced=true;
p[e[i].v]=i;
cur[x]=i;
x=e[i].v;
break;
}
if(!advanced){
int mn=n-1;
for(int i=first[x];i;i=nxt[i])
if(e[i].cap>e[i].flow)mn=min(mn,d[e[i].v]);
if(--num[d[x]]==0)break;
num[d[x]=mn+1]++;
cur[x]=first[x];
if(x^s)x=e[p[x]].u;
}
}
return flow;
}
int main()
{
while(~scanf("%d%d",&m,&n)){
s=1,t=n,ecnt=1;
Link();
memset(d,0,sizeof(d));
memset(p,0,sizeof(p));
printf("%d\n",ISAP());
}
return 0;
}

poj1273 Drainage Ditches的更多相关文章

  1. poj1273 Drainage Ditches Dinic最大流

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 76000   Accepted: 2953 ...

  2. POJ-1273 Drainage Ditches 最大流Dinic

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...

  3. 【网络流】POJ1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78671   Accepted: 3068 ...

  4. 2018.07.06 POJ1273 Drainage Ditches(最大流)

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...

  5. POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

    http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...

  6. POJ1273 Drainage Ditches (网络流)

                                                             Drainage Ditches Time Limit: 1000MS   Memor ...

  7. poj-1273 Drainage Ditches(最大流基础题)

    题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted ...

  8. poj1273 Drainage Ditches (最大流板子

    网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...

  9. [poj1273]Drainage Ditches(最大流)

    解题关键:最大流裸题 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

随机推荐

  1. 10本Java经典书目推荐

    本文列出的10本书是我个人非常喜欢的Java书籍,当我有时间的时候,我就会将它们捧在手里阅读.甚至有些书我反复读过很多遍,每次重新读的时候总会有新的收获.因此这些书也是大部分Java程序员喜欢的书籍. ...

  2. CentOS5.5挂载本地ISO镜像

    操作步骤: 一.挂载iso文件到挂载点 [root@server ~ ]# mount  -o loop /mnt/iso/CentOS5.iso /mnt/cdrom 二.查看挂载状态 [root@ ...

  3. php服务器版本更新工具up2server

    为什么要做这个工具 之前做php 开发的时候,每次版本更新,要把修改的文件同步到服务器,都要小心翼翼,如果漏掉了文件,那就完蛋了,KPI,奖金什么的都没了. 所以写了这个工具.代码在github  h ...

  4. iOS 更改webView文字颜色丶文字大小丶背景色的方法

    在webView的delegate回调方法    - (void)webViewDidFinishLoad:(UIWebView *)webView;中写上一下语句即可 //字体大小 [webView ...

  5. Android音乐播放器源码(歌词.均衡器.收藏.qq5.0菜单.通知)

    一款Android音乐播放器源码,基本功能都实现了 qq5.0菜单(歌词.均衡器.收藏.qq5.0菜单.通知) 只有向右滑动出现,菜单键和指定按钮都还没有添加. 源码下载:http://code.66 ...

  6. 网络婚礼之AFNetWorking3.0

    目前使用人数最多的第三方网络库,没有之一.从开始的NSURLConnection到现在的NSURLSession,它都一直保持着与苹果的步调一致,而由它也衍生出大量的相关第三方网络功能库,不仅仅因为他 ...

  7. iOS 教你如何实现手势密码

    本次讲的手势密码,是在九个按键上实现的,这里讲的是手势密码的基本实现和效果 同样先上效果图 其实就是对画图功能的一个实现,再加上手势操作结合起来 屏幕宽度高度,方便下面操作,不做解释 #define ...

  8. yii2 rbac权限控制之菜单menu详细教程

    作者:白狼 出处:http://www.manks.top/article/yii2_rbac_menu本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...

  9. mysql substring_index substring left right方法

    函数简介: SUBSTRING(str,pos) , SUBSTRING(str FROM pos) SUBSTRING(str,pos,len) , SUBSTRING(str FROM pos F ...

  10. Oracle表字段的增加、删除、修改和重命名

    本文主要是关于Oracle数据库表中字段的增加.删除.修改和重命名的操作. 增加字段语法:alter table tablename add (column datatype [default val ...