Drainage Ditches

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 91585   Accepted: 35493

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

USACO 93

因为这个题要考虑吧,多次对一条边增加流量,所以要用邻接矩阵来处理。这里给出两个代码,当前弧优化,和非当前弧优化版。

#include <iostream>
#include <cstdio>
#include <math.h>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int tab[250][250];//邻接矩阵
int dis[250];//距源点距离,分层图
int cur[280]; //当前弧优化
int N,M;//N:点数;M,边数
queue<int> Q;
int BFS()
{
memset(dis,0xff,sizeof(dis));//以-1填充
dis[1]=0;
Q.push(1);
while (Q.size())
{
int head=Q.front();
Q.pop();
for (int i=1; i<=N; i++)
if (dis[i]<0 && tab[head][i]>0)
{
dis[i]=dis[head]+1;
Q.push(i);
}
}
if (dis[N]>0) return 1;
else return 0;//汇点的DIS小于零,表明BFS不到汇点
}
//dfs代表一次增广,函数返回本次增广的流量,返回0表示无法增广
int dfs(int x,int low)//Low是源点到现在最窄的(剩余流量最小)的边的剩余流量
{
int a=0;
if (x==N)
return low;//是汇点
for (int &i=cur[x]; i<=N; i++)
if (tab[x][i] >0 //联通
&& dis[i]==dis[x]+1 //是分层图的下一层
&&(a=dfs(i,min(low,tab[x][i]))))//能到汇点(a != 0)
{
tab[x][i]-=a;
tab[i][x]+=a;
return a;
}
return 0; }
int dinic()
{
int ans=0,tans;
while (BFS())//要不停地建立分层图,如果BFS不到汇点才结束
{
for(int i=1;i<=N;i++)
cur[i]=1;
while(tans=dfs(1,0x7fffffff))ans+=tans;//一次BFS要不停地找增广路,直到找不到为止
}
return ans;
}
int main()
{
int i,j,f,t,flow,tans;
while (scanf("%d%d",&M,&N)!=EOF)
{
memset(tab,0,sizeof(tab));
for (i=1; i<=M; i++)
{
scanf("%d%d%d",&f,&t,&flow);
tab[f][t]+=flow;
}
printf("%d\n",dinic());
}
}
#include <iostream>
#include <cstdio>
#include <math.h>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int tab[250][250];//邻接矩阵
int dis[250];//距源点距离,分层图
int N,M;//N:点数;M,边数
queue<int> Q;
int BFS()
{
memset(dis,0xff,sizeof(dis));//以-1填充
dis[1]=0;
Q.push(1);
while (Q.size())
{
int head=Q.front();
Q.pop();
for (int i=1; i<=N; i++)
if (dis[i]<0 && tab[head][i]>0)
{
dis[i]=dis[head]+1;
Q.push(i);
}
}
if (dis[N]>0) return 1;
else return 0;//汇点的DIS小于零,表明BFS不到汇点
}
//dfs代表一次增广,函数返回本次增广的流量,返回0表示无法增广
int dfs(int x,int low)//Low是源点到现在最窄的(剩余流量最小)的边的剩余流量
{
int a=0;
if (x==N)
return low;//是汇点
for (int i=1; i<=N; i++)
if (tab[x][i] >0 //联通
&& dis[i]==dis[x]+1 //是分层图的下一层
&&(a=dfs(i,min(low,tab[x][i]))))//能到汇点(a != 0)
{
tab[x][i]-=a;
tab[i][x]+=a;
return a;
}
return 0; }
int dinic()
{
int ans=0,tans;
while (BFS())//要不停地建立分层图,如果BFS不到汇点才结束
{
while(tans=dfs(1,0x7fffffff))ans+=tans;//一次BFS要不停地找增广路,直到找不到为止
}
return ans;
}
int main()
{
int i,j,f,t,flow,tans;
while (scanf("%d%d",&M,&N)!=EOF)
{
memset(tab,0,sizeof(tab));
for (i=1; i<=M; i++)
{
scanf("%d%d%d",&f,&t,&flow);
tab[f][t]+=flow;
}
printf("%d\n",dinic());
}
}

图论-网络流-最大流--POJ1273Drainage Ditches(Dinic)的更多相关文章

  1. 网络流 最大流 Drainage Ditches Dinic

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

  2. 【uva 11082】Matrix Decompressing(图论--网络流最大流 Dinic+拆点二分图匹配)

    题意:有一个N行M列的正整数矩阵,输入N个前1~N行所有元素之和,以及M个前1~M列所有元素之和.要求找一个满足这些条件,并且矩阵中的元素都是1~20之间的正整数的矩阵.输入保证有解,而且1≤N,M≤ ...

  3. 【uva 753】A Plug for UNIX(图论--网络流最大流 Dinic)

    题意:有N个插头,M个设备和K种转换器.要求插的设备尽量多,问最少剩几个不匹配的设备. 解法:给读入的各种插头编个号,源点到设备.设备通过转换器到插头.插头到汇点各自建一条容量为1的边.跑一次最大流就 ...

  4. 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)

    Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...

  5. 图论--网络流--最大流 HDU 2883 kebab(离散化)

    Problem Description Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled ...

  6. 图论--网络流--最大流--POJ 1698 Alice's Chance

    Description Alice, a charming girl, have been dreaming of being a movie star for long. Her chances w ...

  7. 图论--网络流--最大流 POJ 2289 Jamie's Contact Groups (二分+限流建图)

    Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very ...

  8. 图论--网络流--最大流 洛谷P4722(hlpp)

    题目描述 给定 nn 个点,mm 条有向边,给定每条边的容量,求从点 ss 到点 tt 的最大流. 输入格式 第一行包含四个正整数nn.mm.ss.tt,用空格分隔,分别表示点的个数.有向边的个数.源 ...

  9. 图论--网络流--费用流POJ 2195 Going Home

    Description On a grid map there are n little men and n houses. In each unit time, every little man c ...

随机推荐

  1. 五个简单的shell脚本

    1.编写shell脚本 ex1.sh,提示用户输入用户名,并判断此用户名是否存在. (提示:利用read.grep和/etc/passwd) #!/bin/bash echo "请输入用户名 ...

  2. 一个不错的java学习博客

    http://iteye.blog.163.com/blog/static/18630809620131484835129/

  3. JS数据结构与算法 - 剑指offer二叉树算法题汇总

    ❗❗ 必看经验 在博主刷题期间,基本上是碰到一道二叉树就不会碰到一道就不会,有时候一个下午都在搞一道题,看别人解题思路就算能看懂,自己写就呵呵了.一气之下不刷了,改而先去把二叉树的基础算法给搞搞懂,然 ...

  4. 跨平台开源密码管理器 KeePassXC

    简介 KeePassXC 是一个开源的跨平台密码管理器.基于 KeePass 二次开发. KeePassXC 可以安全地在本地存储您的密码,配合浏览器插件KeePassXC-Browser可辅助登录. ...

  5. unity3d之简单动画

    Unity3d中有两个关于动画的概念,Animation和Animator,看一下他们的创建和区别 1.创建一个物体后可以添加Animator和Animation组件如图所示 2.Animation和 ...

  6. Python中赋值、浅拷贝和深拷贝的区别

    前言文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取http: ...

  7. V - Largest Rectangle in a Histogram HDU - 1506

    两种思路: 1 单调栈:维护一个单调非递减栈,当栈为空或者当前元素大于等于栈顶元素时就入栈,当前元素小于栈顶元素时就出栈,出栈的同时计算当前值,当前值所包含的区间范围为从当前栈顶元素到当前元素i的距离 ...

  8. overload 与override的区别

    Override  是重写: 方法名称.参数个数,类型,顺序,返回值类型都是必须和父类方法一致的.它的关系是父子关系Overload 是重载:  方法名称不变,其余的都是可以变更的.它的关系是同一个类 ...

  9. PHP代码审计理解(一)----Metinfo5.0变量覆盖

    0x01 漏洞简介 这个漏洞是metinfo5.0变量覆盖漏洞,并且需要结合文件包含.我使用的cms版本是5.3,事实上已经修复了这个漏洞(5.0的cms源码已经找不到了哈),但是我们可以借他来学习理 ...

  10. 立体匹配-----NCC视差匹配

    目录 一.立体匹配算法 1.立体匹配算法分类 二.NCC 视差匹配方法 1.原理 2.NCC计算公式 3.算法流程 4.代码实现     5.不同场景运行 三.结论 四.遇到的问题及解决方法 一.立体 ...