【47.63%】【hdu 1532】Drainage Ditches
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的更多相关文章
- 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】
利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...
- 【网络流】[USACO4.2]草地排水Drainage Ditches
用EdmondsKarp可过 题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系 ...
- 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...
- 【贪心】【模拟】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个个数在 ...
- 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- 【归并排序】【逆序数】HDU 5775 Bubble Sort
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...
- 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai 的数 ...
随机推荐
- #define 和常量 const 的区别
const 后的常量,程序对其中只能读不能修改. #include <iostream> using namespace std; int main() { const double pi ...
- VIM中空格和TAB的替换
在.vimrc中添加以下代码后,重启vim即可实现按TAB产生4个空格:set ts=4 (注:ts是tabstop的缩写,设TAB宽4个空格)set expandtab 对于已保存的文件,可以使用 ...
- ggplot2笔记
ggplot2笔记 下面是ggplot2的一些文档和github上的源代码http://docs.ggplot2.org/current/https://github.com/hadley/ggplo ...
- 【NS2】TCL debug (转载)
1.使用NS2进行模拟,就不可避免的会接触TCL/OTCL和C/C++.两者配合使用.一般设置场景啊,业务流啊,都使用TCL/OTCL来编写脚 本.要进行路由实验模拟的话,同一类的实验,这些脚本基本上 ...
- ELK3之进阶学习
1.昨日内容回顾 es的基本操作:增删改查 es的两种查询方式: (1)query string (2)query DSL match match match_all sort bool:must,s ...
- Java“类”的内存分配_case1
- @topcoder - SRM697D1L3@ ConnectedStates
目录 @description@ @solution@ @accepted code@ @details@ @description@ 有n个城市,每个城市有个权值wi,任意两个城市i,j之间的道路数 ...
- vue init定制团队模板使用方法
每次做项目都要自己搭建项目目录,或者换了公司就的重新搭建项目目录,是不是很麻烦呢?有没有想过一次性把项目目录搭建好,以后直接用呢?你首先想到的可能是复制自己原来的项目,然后删除.修改等等.然而有个更方 ...
- iptables 网址转译 (Network Address Translation,NAT)
当封包流经NAT電腦時,其位址/通訊端口會被修改,以達到改变包目的地(或旅程),或是让目的地误以为包是源自NAT电脑的效果.換言之,对封包执行NAT的电脑,可以成为新包的来源或目的地,或是成为真正来源 ...
- ubuntu18.04 挂载ntfs硬盘无法写入解决办法
win10和ubuntu18.04双系统,在ubuntu下通过/etc/fstab挂载ntfs硬盘无写入权限,尝试通过chmod修改写入权限和ntfs-config图形工具修改写入权限均失败.在ubu ...