hdu 1532(poj 1273) Drainage Ditches (网络流·最大流)
2015-08-21 19:43
627 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12515 Accepted Submission(s): 5968
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
Sample Output
关键点:有向图,可重边(最大通量累加),小数据量(EK算法)。
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12515 Accepted Submission(s): 5968
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
关键点:有向图,可重边(最大通量累加),小数据量(EK算法)。
#include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace std; const int N=210,INF=0x3f3f3f3f; int n,m; int map ,p ; bool flag ; bool EK_bfs(int start,int end){ queue<int> q; memset(flag,0,sizeof(flag)); memset(p,-1,sizeof(p)); q.push(start); flag[start]=1; while(!q.empty()){ int e=q.front(); if(e==end) return 1; q.pop(); for(int i=1;i<=m;i++){ if(map[e][i]&&!flag[i]){ flag[i]=1; p[i]=e; q.push(i); } } } return 0; } int EK_maxflow(int start,int end){ int u,ans=0,temp; while(EK_bfs(start,end)){ temp=INF; u=end; while(p[u]!=-1){ temp=min(temp,map[p[u]][u]); u=p[u]; } ans+=temp; u=end; while(p[u]!=-1){ map[p[u]][u]-=temp; map[u][p[u]]+=temp; u=p[u]; } } return ans; } int main() { //freopen("cin.txt","r",stdin); while(cin>>n>>m){ int a,b,c; memset(map,0,sizeof(map)); for(int i=0;i<n;i++){ scanf("%d%d%d",&a,&b,&c); map[a][b]+=c; } printf("%d\n",EK_maxflow(1,m)); } return 0; }
相关文章推荐
- 对开源C++网络库简单应用总结
- UFLDL教程(四)---深度网络
- 简单封装的httpclient
- 【Android】网络通讯
- linux网络编程学习笔记之四 -----多-threaded服务器
- WebService只能在本地使用,无法通过网络访问的解决办法
- HTTP状态码及Msg定义
- HTTP返回状态值及用途
- Firebug折腾记_(4)响应时间监测
- httpmime-4.1.3.jar和httpmime-4.3.6.jar 发送的http请求有差异,导致服务端无法接收到数据。
- Android利用Http下载文件
- TCP/IP-ARP
- win10怎么新建网络映射驱动器?win10映射驱动器的使用方法
- solr4.9.1索引导入http远程doc文件
- 计算机网络 之 IP地址、子网掩码与默认网关
- HTTPS 背后的加密算法
- tcpkill清除异常tcp连接
- TCP、UDP、Socket 通信(原)
- 网络爬虫:分离生产者和消费者来优化爬虫程序
- 网络爬虫:分离生产者和消费者来优化爬虫程序