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

Total Submission(s): 15777 Accepted Submission(s): 7514

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

【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=1532

【题解】



让你找从点1到点m的最大流;

用那个E-K算法搞增广路做就好;

用广搜来搞;

路上的边权是剩余网络的边权;

每次修改a后;

正向边+a;

反向边-a;

一开始输入的z是这个网络上边的容量限制(一开始没有任何流);

所以每次修改a后就直接增加答案a;

中途跳出队列可能dl不为空,所以要清空队列;

如果还能找到增广路就继续找;继续增加最大流;



【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long using namespace std; const int MAXN = 300;
const int INF = 2100000000;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0); int n,m;
int pre[MAXN];
int flow[MAXN][MAXN];
bool mark[MAXN];
vector <int> a[MAXN];
queue <int>dl; void read2(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void read1(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
while (~scanf("%d%d",&n,&m))
{
memset(flow,0,sizeof(flow));
for (int i = 1;i <= n;i++)
{
int x,y,z;
read1(x);read1(y);read1(z);
flow[x][y]+=z;
}
int f = 0;
while (true)
{
memset(mark,false,sizeof(mark));
memset(pre,0,sizeof(pre));
while (!dl.empty())
dl.pop();
dl.push(1);
mark[1] = true;
while (!dl.empty())
{
int x = dl.front();
dl.pop();
if (x==m)
break;
for (int i = 1;i <= m;i++)
if (flow[x][i]>0 && !mark[i])
{
mark[i] = true;
pre[i] = x;
dl.push(i);
}
}
if (!mark[m])
break;
int mi = INF;
int i = m;
while (i!=1)
{
mi = min(mi,flow[pre[i]][i]);
i = pre[i];
}
if (mi==INF)
break;
i = m;
while (i!=1)
{
flow[pre[i]][i]-=mi;
flow[i][pre[i]]+=mi;
i = pre[i];
}
f+=mi;
}
cout << f<<endl;
}
return 0;
}

【47.63%】【hdu 1532】Drainage Ditches的更多相关文章

  1. 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】

    利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...

  2. 【网络流】[USACO4.2]草地排水Drainage Ditches

    用EdmondsKarp可过 题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系 ...

  3. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  4. 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  5. 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...

  6. 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...

  7. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  8. 【归并排序】【逆序数】HDU 5775 Bubble Sort

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...

  9. 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai  的数 ...

随机推荐

  1. select标签模拟placeholder属性与一般操作(最重要的是ios某一项被选中的兼容)

    1.为了统一样式,常常要模拟placeholder <select> <option disabled selected hidden>请选择</option> & ...

  2. Libevent:9Evbuffers缓存IO的实用功能

    Libevent的evbuffer功能实现了一个字节队列,优化了在队列尾端增加数据,以及从队列前端删除数据的操作. Evbuffer用来实现缓存网络IO中的缓存部分.它们不能用来在条件发生时调度IO或 ...

  3. centos安装php7.18注意

    报错–php53-common conflicts with php-common //解决 yum -y install php* --skip-broken 第一步:安装源 yum install ...

  4. python生成器和各种推导式

    一. 生成器 本质就是迭代器. 一个一个的创建对象 创建生成器的方式: 1. 生成器函数 2. 通过生成器表达式来获取生成器 3. 类型转换(看不到) 二. 生成器函数 (重点) 生成器函数中包含 y ...

  5. oralce GROUPING

    /*从上面的结果中我们很容易发现,每个统计数据所对应的行都会出现null, 如何来区分到底是根据那个字段做的汇总呢,grouping函数判断是否合计列!*/ select decode(groupin ...

  6. docker oracle install

    https://hub.docker.com/r/9fevrier/oracle-11g Informations Oracle directory : /opt/oracle Data direct ...

  7. Python基础:20类的定制

    类中有一些可自定义的特殊方法,它们中的一些有预定义的默认行为,而其它一些则没有,留到需要的时候去实现.这些特殊方法是Python中用来扩充类的强有力的方式.它们可以实现模拟标准类型和重载操作符等.比如 ...

  8. vue element 给指数的div加loading

     const loading = this.$loading({       lock: true,       text: 'Loading',       spinner: 'el-icon-lo ...

  9. HZOJ string

    正解炸了…… 考试的时候想到了正解,非常高兴的打出来了线段树,又调了好长时间,对拍了一下发现除了非常大的点跑的有点慢外其他还行.因为复杂度算着有点高…… 最后正解死于常数太大……旁边的lyl用同样的算 ...

  10. [ZJOI2007] 小Q的矩阵游戏 (模板—Dinic)

    B. 矩阵游戏 题目描述 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行 ...