HDU1532——Drainage Ditches(网络流Dinic算法)
来源:互联网 发布:linux 显示文件夹内容 编辑:程序博客网 时间:2024/06/05 20:01
Drainage Ditches
点击打开链接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): 14961 Accepted Submission(s): 7109
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 41 2 401 4 202 4 202 3 303 4 10
Sample Output
50解题思路:这道题用EK或者Dinic算法都可以,这里小龙人用的是Dinic算法。首先建造地图给每个节点分层(比如 1->2, 2->4, 1->3, 3->4, 2->3 这个图,1是0层,2,3是1层,4是2层),每个节点只能走向下一层的节点,并且这两个节点有水管。分层分好后,比如有两条路,依次走这两条路,选出每条路最小的水管容量,然后用这条路所有水管容量依次减去最小水管容量,并且添加反向容量(反向容量=该水管的流量,至于为什么要添加,这里比较难懂,小龙人建议去问问身边的大神),返回最小水管容量,两条路走完后,再次给地图节点分层(这时,流量达到容量的水管已经为0,或者说这条路已经不通了),重复上面的步骤,最后直到分层后不能给汇点分层就结束。代码实现:#include <iostream>#include <cstdio>#include <cstring>#include <set>#include <queue>#include <vector>using namespace std;const int maxn = 205;const int MAXN = ( 1<<29 );int m, n;int mp[maxn][maxn];int dis[maxn];bool bfs( int d ) //用bfs给图分层;{ queue<int>q; memset(dis, -1, sizeof(dis)); dis[d] = 0; q.push(d); while( !q.empty() ) { int top = q.front(); q.pop(); for( int j=1; j<=n; j++ ) { if( dis[j] == -1 && mp[top][j] > 0 ) { dis[j] = dis[top] + 1; //层数+1; q.push(j); } } } if( dis[n] > 0 ) //直到汇点不能分层结束; return true; return false;}int findd( int k, int low ){ if( k == n ) return low; int a = 0; for( int i=1; i<=n; i++ ) { if( dis[i] == dis[k]+1 && mp[k][i] > 0 && ( a = findd( i, min(low, mp[k][i]) )) ) // 用递归找到该路的最小容量; { mp[k][i] -= a; // 该路各水管依次减去最小容量; mp[i][k] += a; // 添加反向容量; return a; } } return 0;}int main(){ while( scanf("%d%d",&m,&n)!=EOF ) { memset(mp, 0, sizeof(mp)); int si, ei, ci; for( int i=0; i<m; i++ ) { scanf("%d%d%d",&si,&ei,&ci); mp[si][ei] += ci; // 细节注意,这里必须用叠加,有可能 1->2有两根水管叠加; } int ans = 0, temp; while(bfs(1)) { while( temp = findd( 1, MAXN ) ) ans += temp; } printf("%d\n",ans); } return 0;}
- HDU1532——Drainage Ditches(网络流Dinic算法)
- HDU1532 Drainage Ditches Dinic算法
- HDU1532 Drainage Ditches 网络流之dinic算法(省内存,但耗时)
- hdu1532——Drainage Ditches(网络流模板)
- HDU1532 Drainage Ditches —— 最大流(sap算法)
- hdu1532 Drainage Ditches (Dinic)
- 最大网络流dinic算法—【hdoj1532】Drainage Ditches
- 增广路算法(网络流) HDU1532 Drainage Ditches
- Drainage Ditches(HDU1532,网络流)
- HDU1532 Drainage Ditches 最大流Dinic模板
- HDU1532 - Drainage Ditches(网络流)
- POJ 1273 Drainage Ditches(网络流【Dinic算法】)
- HDU1532 Drainage Ditches 网络流之SAP算法
- HDU1532 Drainage Ditches (网络流基础水题)
- HDU1532 Drainage Ditches(网络流模板)
- HDU1532 Drainage Ditches 网络流模板
- HDU1532 Drainage Ditches(网络流EdmondsKarp)
- HDU1532 Drainage Ditches(网络流、EdmondsKarp)
- Educational Codeforces Round 15 C. Cellular Network
- unity MonoBehavior 函数执行顺序和生命周期
- 最少钱币数
- Crafting Winning Solutions
- <LeetCode> 题31:数组划分
- HDU1532——Drainage Ditches(网络流Dinic算法)
- 在霍格沃茨找零钱
- OSI七层协议每层的代表性协议举例
- 欢迎使用CSDN-markdown编辑器
- 个位数统计
- spring mvc 上传文件
- HDU 5777 domino
- POJ 2096 Collecting Bugs (基础概率DP)
- A除以B