Drainage Ditches(最大流入门)
发布日期:2021-09-08 01:44:45 浏览次数:33 分类:技术文章

本文共 3838 字,大约阅读时间需要 12 分钟。

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5649    Accepted Submission(s): 2666

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
 

 

Source
 

 

Recommend
lwg
 

最大流入门题,参考资料:

1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 7 const int maxn = 201; 8 int n, m; //n:路径数,m:顶点数 9 int r[maxn][maxn]; //剩余容量,初始值为最大容量10 bool vis[maxn]; //在搜寻增广路径时记录已访问顶点11 int pre[maxn]; //pre[i]记录前驱顶点,以便记录增广路径中的顶点。12 //注意的是不能记录后继顶点,因为在广搜中,顶点的前驱唯一而后继不唯一13 14 bool BFS(int s, int t) //s:源点,t:汇点15 {16 memset(vis, false, sizeof(vis));17 memset(pre, -1, sizeof(pre));18 queue
que;19 que.push(s);20 while(!que.empty())21 {22 int v = que.front();23 vis[v] = true;24 que.pop();25 for(int i = 1; i <= m; i++) //注意顶点由1开始标记26 {27 if(r[v][i] && !vis[i])28 {29 pre[i] = v;30 if(i == t) return true; //已找到增广路径31 que.push(i);32 }33 }34 }35 return false;36 }37 38 int EK(int s, int t) //s:源点,t:汇点39 {40 int max_flow = 0, d; //max_flow:整个网络的最大流,d:为增广路径带来的增量41 while(BFS(s, t))42 {43 int i = pre[t], j; //设置i,j只是为了敲代码方便44 d = r[i][t];45 for(; i != s; i = j) //获得增量d46 {47 j = pre[i];48 if(d > r[j][i]) d = r[j][i];49 }50 for(i = t; i != s; i = j) //求残流网络51 {52 j = pre[i];53 r[j][i] -= d;54 r[i][j] += d;55 }56 max_flow += d;57 }58 return max_flow;59 }60 61 int main()62 {63 while(scanf("%d %d", &n, &m) != EOF)64 {65 int s, e, c;66 memset(r, 0, sizeof(r));67 while(n--)68 {69 scanf("%d %d %d", &s, &e, &c);70 r[s][e] += c; //不直接输入r[s][e],预防有重边!71 }72 printf("%d\n", EK(1, m));73 }74 return 0;75 }

 

 |   |   | 

转载地址:https://blog.csdn.net/weixin_34310369/article/details/85553041 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:逻辑地址和物理地址
下一篇:《今日推荐》值得一试的8个 Web 开发工具和资源

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月02日 18时39分31秒