TZOJ 4085 Drainage Ditches(最大流)
描述
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.
输入
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.
输出
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
样例输入
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
样例输出
50
题意
M条边N个点,M行每行u,v,w,代表从u到v的最大流量w
题解
直接建图跑最大流,入门题
代码
#include<bits/stdc++.h>
using namespace std; const int N=,M=;
int c[N][N],pre[N],n,m,S,T;
bool bfs()
{
int vis[N]={};
memset(pre,,sizeof pre);
queue<int>q;
q.push(S);
while(!q.empty())
{
int u=q.front();q.pop();
for(int v=;v<=n;v++)
{
if(c[u][v]>&&!vis[v])
{
pre[v]=u;
if(v==n)return true;
vis[v]=;
q.push(v);
}
}
}
return false;
}
int maxflow()
{
int flow=;
while(bfs())
{
int d=1e9;
for(int i=T;i!=;i=pre[i])d=min(d,c[pre[i]][i]);
for(int i=T;i!=;i=pre[i])
c[pre[i]][i]-=d,
c[i][pre[i]]+=d;
flow+=d;
}
return flow;
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(c,,sizeof c);
for(int i=,u,v,w;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
c[u][v]+=w;
}
S=,T=n;
printf("%d\n",maxflow());
}
return ;
}
TZOJ 4085 Drainage Ditches(最大流)的更多相关文章
- Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )
题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...
- poj 1273 Drainage Ditches 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)
Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- HDU1532 Drainage Ditches —— 最大流(sap算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/ ...
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- hdu 1532 Drainage Ditches(最大流)
Drainage Dit ...
- POJ-1273 Drainage Ditches 最大流Dinic
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
随机推荐
- leetcode 题解: Gray Code
第一眼看到就是枚举,回溯法. n位的ans就是在n-1的ans的基础上,每一个在首位加上1. 但是有个难点,要保证相邻两数之间只有一位在变化,怎么办? 首先 00 00 01 00 01 11 10 ...
- native.js 判断是否安装某app
例:是否安装微信 function isWeixin() { var UIApplication = plus.ios.importClass("UIApplication"); ...
- 1016B - Segment Occurrences(字符串的匹配)
题意:字符串a,字符串b,给你q个区间,让你求a的区间内字符串b出现了多少次 之前用的前缀数组做的,没想起来,发现这个其实也可以 #include<cstdio> #include< ...
- idea安装下载
https://blog.csdn.net/qq_41983010/article/details/82562975
- 使用exec函数将当前的信息输入到文件中
先来看看exec函数: exec函数族 fork创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支),子进程往往要调用一种exec函数以执行另一个程序.当进程调用一种exec函 ...
- Python 3 学习笔记(3)
模块 编写模块 # fibo.py # Fibonacci numbers module def fib(n): # write Fibonacci series up to n a, b = 0, ...
- ReactiveX 学习笔记(2)创建数据流
操作符(Operators) Rx 的操作符能够操作(创建/转换/组合) Observable. Creating Observables 本文主题为创建/生成 Observable 的操作符. 这里 ...
- Extjs获取Form中的数据
var win = Ext.create("Ext.window.Window",{ width:300, height:200, title:"日期选择窗口" ...
- C# DataTable使用方法详解--删除表数据
在项目中常常常使用到DataTable,假设DataTable使用得当,不仅能使程序简洁有用,并且可以提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 1.添加引用 1 u ...
- Android中查看SQLite中字段数据的两种方式
方式一:ADB Pull 通过adb pull导出*.db文件到PC的文件夹中,通过可视化工具 SQLiteExpertPers 进行查看.编辑: adb pull /data/data/com.jo ...