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. C语言输出 1到20 的阶乘之和

    除了调用库,绝对找不到比这更精简的代码了. #include<stdio.h> #include<string.h> long long getdata(long long n ...

  2. Linux网络篇,ssh原理及应用

    一.对称加密与非对称加密 对称加密: 加密和解密的秘钥使用的是同一个.    非对称加密: 非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥:简称公钥和私钥 对称加密 对称加密的密 ...

  3. Python 1基础语法二(标识符、关键字、变量和字符串)

    一.标识符 标识符就是程序员自己命名的变量名.名字需要有见名知义的效果,不要随意起名 :比如 a=1 a是个变量,a这个变量名属于标识符 1 company = '小米 2 employeeNum = ...

  4. 用ASP.NET MVC5 +SQLSERVER2014搭建多层架构的数据库管理系统

    用http://ASP.NET MVC5 +SQLSERVER2014搭建多层架构的数据库管理系统 背景:前段时间,给一家公司做外包(就是图标是朵菊花那家).为了尽快实现交付,网上四处寻找适合中小型企 ...

  5. AJ学IOS 之微博项目实战(13)发送微博调用相机里面的图片以及调用相机

    AJ分享,必须精品 一:效果 二:代码 相机部分就简单多了,几行代码调用而已,但是如果你要是想实现更多丰富的功能,需要自己写.利用AssetsLibrary.framework,利用这个框架可以获得手 ...

  6. 【翻译】创建String 使用“”还是构造函数(new String)

    在java中创建String,通常有以下两种方法. String x = "abc"; String y = new String("abc"); 它们之间有什 ...

  7. JMF 下载安装与测试 测试成功

    本来就是想在自己写的java里面加入实习的摄像头监控,然后个各种百度了一下,就用JMF来弄了,不过这个东西貌似比较旧,网上的资料虽然说有,但是也不是太多,并且遇到的一下问题也不能解决,总之经过了一天的 ...

  8. stand up meeting 12-11

    今天因组员时间问题,并没有集中在一起开会,但士杰当面和天赋同学进行了沟通,在lync与国庆进行了沟通. 天赋与重阳再次进行了了沟通,确定了“单词挑战”与“背单词”这两个模块集成的难度,决定先不进行集成 ...

  9. 前后端分离下用jwt做用户认证

    0 前后端分离下的用户信息认证 前端使用Vue+axios,后端使用SpringBoot+SpringSecurity. 为了解决http无状态的问题,我采用jwt(json web token)保存 ...

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

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