网络流。。

来源:互联网 发布:2016网络零售交易额 编辑:程序博客网 时间:2024/05/17 20:31


Description

Every time itrains on Farmer John's fields, a pond forms over Bessie's favorite cloverpatch. This means that the clover is covered by water for awhile and takesquite a long time to regrow. Thus, Farmer John has built a set of drainageditches so that Bessie's clover patch is never covered in water. Instead, thewater is drained to a nearby stream. Being an ace engineer, Farmer John hasalso installed regulators at the beginning of each ditch, so he can control atwhat rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transportper minute but also the exact layout of the ditches, which feed out of the pondand into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can betransported out of the pond and into the stream. For any given ditch, waterflows in only one direction, but there might be a way that water can flow in acircle. 

Input

The input includesseveral cases. For each case, the first line contains twospace-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 ofintersections points for those ditches. Intersection 1 is the pond.Intersection point M is the stream. Each of the following N lines containsthree integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designatethe intersections between which this ditch flows. Water will flow through thisditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate atwhich water will flow through the ditch.

Output

For each case,output a single integer, the maximum rate at which water may emptied from thepond.

Sample Input

5 4

1 2 40

1 4 20

2 4 20

2 3 30

3 4 10

Sample Output

50

题意

现在有m个池塘(1m开始编号,1为源点,m为汇点),n条水渠,给出这n条水渠所连接的点和所能流过的最大流量,求最大流量。



FF算法,可以用dinic优化,这里先讲原版FF,这个题目图的存储很重要,在存上本身的边的同时,还需要存上权值为0的反向边来记录后来每次求得增广路所花的容积。图存储完成后自定义函数进入原点和汇点,定义一个变量与数组来记录每条增广路的流量与访问标记(注意要在此是while循环,每次dfs搜索之前要先把标记数组的值清零),接着在dfs中遍历边(dinic即是将图用bfs先分层提高效率)每找到一条路就把正边花的流量加在反向边上,然后返回。

0 0
原创粉丝点击