洛谷 P2740 [USACO4.2] 草地排水Drainage Ditches [Edmonds-Karp算法]
来源:互联网 发布:博雅软件上市进展 编辑:程序博客网 时间:2024/05/16 02:14
题目背景
在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水。这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间。因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没的烦恼(不用担心,雨水会流向附近的一条小溪)。作为一名一流的技师,农夫约翰已经在每条排水沟的一端安上了控制器,这样他可以控制流入排水沟的水流量。
题目描述
农夫约翰知道每一条排水沟每分钟可以流过的水量,和排水系统的准确布局(起点为水潭而终点为小溪的一张网)。需要注意的是,有些时候从一处到另一处不只有一条排水沟。
根据这些信息,计算从水潭排水到小溪的最大流量。对于给出的每条排水沟,雨水只能沿着一个方向流动,注意可能会出现雨水环形流动的情形。
输入输出格式
输入格式:第1行: 两个用空格分开的整数N (0 <= N <= 200) 和 M (2 <= M <= 200)。N是农夫John已经挖好的排水沟的数量,M是排水沟交叉点的数量。交点1是水潭,交点M是小溪。
第二行到第N+1行: 每行有三个整数,Si, Ei, 和 Ci。Si 和 Ei (1 <= Si, Ei <= M) 指明排水沟两端的交点,雨水从Si 流向Ei。Ci (0 <= Ci <= 10,000,000)是这条排水沟的最大容量。
输出格式:输出一个整数,即排水的最大流量。
输入输出样例
输入样例#1:
5 41 2 401 4 202 4 202 3 303 4 10
输出样例#1:
50
说明
题目翻译来自NOCOW。
USACO Training Section 4.2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
网络流增广路~
直接套模板就可以了~
#include<cstdio>#include<cstring>#include<iostream>#include<queue>#include<vector>using namespace std;int n,m,x,y,v,a[401],ne[401],ans;struct edge{int x,y,cap,flow;edge(int u,int v,int k,int z):x(u),y(v),cap(k),flow(z) {}};vector<edge> ed;vector<int> p[401];int main(){scanf("%d%d",&m,&n);for(int i=1;i<=m;i++){scanf("%d%d%d",&x,&y,&v);ed.push_back(edge(x,y,v,0));ed.push_back(edge(y,x,0,0));int tot=ed.size();p[x].push_back(tot-2);p[y].push_back(tot-1);}while(1){memset(a,0,sizeof(a));queue<int> q;q.push(1);a[1]=999999999;while(!q.empty()){int k=q.front();q.pop();int tot=p[k].size();for(int i=0;i<tot;i++){edge z=ed[p[k][i]];if(!a[z.y] && z.cap-z.flow>0){ne[z.y]=p[k][i];a[z.y]=min(a[k],z.cap-z.flow);q.push(z.y);}}if(a[n]) break;}if(!a[n]) break;for(int i=n;i!=1;i=ed[ne[i]].x){ed[ne[i]].flow+=a[n];ed[ne[i]^1].flow-=a[n];}ans+=a[n];}printf("%d\n",ans);return 0;}
原题好像是多组数据,这个是HDU上A的~
#include<cstdio>#include<cstring>#include<iostream>#include<queue>#include<vector>using namespace std;int n,m,x,y,v,a[401],ne[401],ans;struct edge{int x,y,cap,flow;edge(int u,int v,int k,int z):x(u),y(v),cap(k),flow(z) {}};vector<edge> ed;vector<int> p[401];int main(){while(scanf("%d%d",&m,&n)!=EOF){ans=0;ed.clear();for(int i=0;i<=n;i++) p[i].clear();for(int i=1;i<=m;i++){scanf("%d%d%d",&x,&y,&v);ed.push_back(edge(x,y,v,0));ed.push_back(edge(y,x,0,0));int tot=ed.size();p[x].push_back(tot-2);p[y].push_back(tot-1);}while(1){memset(a,0,sizeof(a));queue<int> q;q.push(1);a[1]=999999999;while(!q.empty()){int k=q.front();q.pop();int tot=p[k].size();for(int i=0;i<tot;i++){edge z=ed[p[k][i]];if(!a[z.y] && z.cap-z.flow>0){ne[z.y]=p[k][i];a[z.y]=min(a[k],z.cap-z.flow);q.push(z.y);}}if(a[n]) break;}if(!a[n]) break;for(int i=n;i!=1;i=ed[ne[i]].x){ed[ne[i]].flow+=a[n];ed[ne[i]^1].flow-=a[n];}ans+=a[n];}printf("%d\n",ans);}return 0;}
1 0
- 洛谷 P2740 [USACO4.2] 草地排水Drainage Ditches [Edmonds-Karp算法]
- 洛谷 P2740 [USACO4.2] 草地排水Drainage Ditches [dinic算法]
- P2740 [USACO4.2]草地排水Drainage Ditches
- 【题解】洛谷P2740 poj1273 [USACO4.2]草地排水Drainage Ditches
- 【USACO4.2】草地排水Drainage Ditches(最大流)
- 【Drainage Ditches 草地排水】
- hdu 1532 Drainage Ditches(Edmonds-Karp最大流算法)
- 【POJ 1273 Drainage Ditches】 & 网络流 & Edmonds-Karp算法
- USACO 4.2 草地排水 Drainage Ditches
- USACO Training 4.2.1 Drainage Ditches 草地排水 题解与分析<网络流DINIC算法>
- 【USACO题库】4.2.1 Drainage Ditches草地排水
- USACO 4.2 Drainage Ditches 草地排水(预流推进)
- POJ 1273 Drainage Ditches (最大流问题——Edmonds-Karp算法)
- [USACO4.2]草地排水(网络流最大流模板)
- 网络流的初步应用[USACO4.2]草地排水
- usaco4.2.1 Drainage Ditches
- [USACO4.2.1]Drainage Ditches
- 网络流Sap+Gap(【USACO题库】4.2.1 Drainage Ditches草地排水 )
- CSS使用网络字体(@font-face)详析
- Nginx + Tomcat 动静分离实现负载均衡
- [Andriod]Andriod之HelloWorld
- UE4中的集合:TSet容器
- TextView/EditText在ListView无法长按复制
- 洛谷 P2740 [USACO4.2] 草地排水Drainage Ditches [Edmonds-Karp算法]
- 计算组合数
- 检测字符串中是否包含汉字
- 数据可视化的工具
- Android自定义流式布局-FlowLayout
- has_many 和belongs_to
- 检测字符串中只能包含:中文、数字、下划线(_)、横线(-)
- 1004. Counting Leaves (30)
- 判断浏览器版本过低代码