poj 1273 Drainage Ditches 【图论-网络流-最大流-Ford-Fulkerson】

来源:互联网 发布:sai mac版打不开 编辑:程序博客网 时间:2024/05/14 09:56
                            Drainage Ditches                Time Limit: 1000MS      Memory Limit: 10000K

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

知识点:网络流-最大流

题目大意:一个农场主为了灌溉便利,在各个农场点挖掘沟渠,每条沟渠都有固定的容量,让你求出整个工程的最大流量

AC代码:

//简单易懂的Ford-Fulkerson算法# include <cstdio># include <cstring># define MAXN 305# define INF 1000000000int n, m;int map[MAXN][MAXN];int used[MAXN];int Dfs(int s, int t, int f){    if (s == t)    {        return f;    }    for (int i = 1; i <= n; i++)    {        if (map[s][i] > 0 && !used[i])        {            used[i] = 1;            int minf = f < map[s][i] ? f : map[s][i];            int d = Dfs(i, t, minf);            if (d > 0)            {                map[s][i] -= d;                map[i][s] += d;            //  printf("%d -> %d : %d\n", s, i, d);                return d;            }        }    }    return 0;}int Maxflow(int s, int t){    int flow = 0;    while (1)    {        memset(used, 0, sizeof(used));        int f = Dfs(s, t, INF);        if (!f)        {            return flow;        }        flow += f;    }}int main(void){    while (~scanf("%d %d", &m, &n))    {        memset(map, 0, sizeof(map));        for (int i = 0; i < m; i++)        {            int u, v, w;            scanf("%d %d %d", &u, &v, &w);            if (u == v)            {                continue;            }            map[u][v] += w; //有可能两点有多条沟渠的情况        }        printf("%d\n", Maxflow(1, n));    }    return 0;}
0 0
原创粉丝点击