HDU 1532(网络流之最大流)

来源:互联网 发布:中国网络广播电台 编辑:程序博客网 时间:2024/05/21 07:56

Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15400 Accepted Submission(s): 7336

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
题意:农民休水渠通道,通道个数m,与田 n,然后就是m 个通道的起始位置 a, 到达位置 b,和流量 c,问怎么样使得 源点 1 到汇点 n 的流量最大。
题解:最大流的模板题,点此链接,把前面的看看了解一下专业名词,然后看着代码跟着思路,理解一下模板就懂了好多。
我的理解呢,最大流就是水从源点出发,经历很多的点到达汇点(也就是终点),每两个点之间有通道,通道大小不同,连接的方式也不同,最大流就是求怎么让水的流通量最大。

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;#define M 220#define INF 0x3f3f3f3f#define CRL(a, b) memset(a, b, sizeof(a))int mp[M][M];//表示通道的容量 int max_flow(int n, int mp[][M], int source, int sink){    int pre[M], min_flow[M], path[M][M], ans = 0;    //path 表示当前流量(已存在的通道) , min_flow 表示当前连通管道的最小流量     CRL(path, 0);    CRL(min_flow, 0);    while(1)    {        CRL(pre, -1);//更新父节点         pre[source] = -2;//特定设一个值,防止重复连接         min_flow[source] = INF;//源点的流量可用无穷大         queue<int> q;        q.push(source);        while(!q.empty())        {            int temp = q.front();            q.pop();            for(int i=1; i<=n; i++)            {                if(pre[i] == -1 && path[temp][i] < mp[temp][i])//i 点没有被探索,并且还有可用流量                 {                    pre[i] = temp;                    q.push(i);                    min_flow[i] = min(min_flow[temp], mp[temp][i] - path[temp][i]);                    //可用流量与这条通道的最小流量对比,取最小的那个                 }            }            if(pre[sink] != -1)//当汇点被连通的时候             {                int k = sink;                while(pre[k] > 0)                {                    path[pre[k]][k] += min_flow[sink];//将这一通道的每一个当前流量更新                    //,注意更新是加上与减去的都是该通道的 最小值                     path[k][pre[k]] -= min_flow[sink];//这个暂时不懂,估计是为了防止某些通道的忽略                     k = pre[k];                }                break;            }        }        if(pre[sink] != -1)        {            ans += min_flow[sink];//结果加上该通道的最小流量         }        else        {            return ans;        }    }}int main(){    int a, b, c, m, n;    while(scanf("%d%d", &m, &n) != EOF)    {        CRL(mp, 0);        for(int i=0; i<m; i++)        {            scanf("%d%d%d", &a, &b, &c);            mp[a][b] += c;        }        int ans = max_flow(n, mp, 1, n);        printf("%d\n", ans);    }    return 0;}
0 0
原创粉丝点击