nyoj 223 Drainage Ditches 【网络流&&最大流】
2016-01-11 20:59
423 查看
难度:4
描述 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.
样例输入
样例输出
[/code]
Drainage Ditches
时间限制:1000 ms | 内存限制:65535 KB难度:4
描述 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
分析:
最大流模版,谨以此记住。
代码:
#include<cstdio> #include<cstdlib> #include<algorithm> #include<queue> #include<cstring> #include<stack> #include<iostream> #include<vector> #define mem(x,y) memset(x,y,sizeof(x)) #define SI(x) scanf("%d",&x) #define O_O(x) while(x--) using namespace std; const int maxn=220; const int INF=0x3f3f3f3f; int map[maxn][maxn]; int dep[maxn]; int n,m,src,des; int bfs() { queue<int>q; while(!q.empty()) q.pop(); mem(dep,-1); dep[src]=0; q.push(src); while(!q.empty()) { int u=q.front(); q.pop(); for(int v=1;v<=m;v++) if(map[u][v]>0&&dep[v]==-1) { dep[v]=dep[u]+1; q.push(v); } } return dep[des]!=-1; } int dfs(int u,int minx) { if(u==des) return minx; int tmp; for(int v=1;v<=m;v++) { if(map[u][v]>0&&dep[v]==dep[u]+1&&(tmp=dfs(v,min(minx,map[u][v])))) { map[u][v]-=tmp; map[v][u]+=tmp; return tmp; } } return 0; } int dinic() { int ans=0,temp; while(bfs()){ while(1) { temp=dfs(1,INF); if(temp==0) break; ans+=temp; } } return ans; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { mem(map,0); int u,v,w; for(int i=0;i<n;i++) { scanf("%d%d%d",&u,&v,&w); map[u][v]+=w; } src=1,des=m; printf("%d\n",dinic()); } return 0; }
[/code]
相关文章推荐
- 通讯系统经验谈【一】TCP连接状态分析:SYNC_RECV,CLOSE_WAIT,TIME_WAIT
- UR的TCP通讯
- 使用TCP协议的NAT穿透技术 (转载)
- 【jsp】The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path
- 阿岳之_httpd2.2网页认证和虚拟主机配置篇(一)
- 启动一个支持文件上传的HTTP-Server
- HDU 4265 Science! 网络流解多重匹配,输出所有比配可能
- http协议
- Android:Https跳过证书验证、Https使用证书、HttpClient、
- 虚拟机VMware3种网络模式(桥接、nat、Host-only)的工作原理
- 使用套接字Socked实现网络通讯(网络编程)
- 【解决方案】Http请求报错
- 从僵尸网络追踪到入侵检测 第6章 Honeyd日志使用
- 关于http接口防止非法调用的设计方案
- 网络通信的工作原理
- 反向传播神经网络 BPNN
- OpenCV中使用神经网络 CvANN_MLP
- BZOJ 1189: [HNOI2007]紧急疏散evacuate|网络流|二分答案
- 使用okhttp异步下载图片,保存到本地,并在系统相册中显示
- iOS开发网络篇之文件下载、大文件下载、断点下载