版权声明:转载请注明出处。 https://blog.csdn.net/u014427196/article/details/46383243

模板题。
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

代码:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>  
#include <ctype.h>  
#include <algorithm>  
#include <vector>  
#include <string.h>  
#include <queue>  
#include <stack>  
#include <set>  
#include <map>  
#include <string>  
#include <sstream>  
#include <malloc.h>

using namespace std;
const int MAXN = 1010;//点数的最大值
const int MAXM = 400010;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{
    int to, next, cap, flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN];
void init()
{
    tol = 0;
    memset(head, -1, sizeof(head));
}
//加边,单向图三个参数,双向图四个参数
void addedge(int u, int v, int w, int rw = 0)
{
    edge[tol].to = v; edge[tol].cap = w; edge[tol].next = head[u];
    edge[tol].flow = 0; head[u] = tol++;
    edge[tol].to = u; edge[tol].cap = rw; edge[tol].next = head[v];
    edge[tol].flow = 0; head[v] = tol++;
}
//输入参数:起点、终点、点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start, int end, int N)
{
    memset(gap, 0, sizeof(gap));
    memset(dep, 0, sizeof(dep));
    memcpy(cur, head, sizeof(head));
    int u = start;
    pre[u] = -1;
    gap[0] = N;
    int ans = 0;
    while (dep[start] < N)
    {
        if (u == end)
        {
            int Min = INF;
            for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to])
                if (Min > edge[i].cap - edge[i].flow)
                    Min = edge[i].cap - edge[i].flow;
            for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to])
            {
                edge[i].flow += Min;
                edge[i ^ 1].flow -= Min;
            }
            u = start;
            ans += Min;
            continue;
        }
        bool flag = false;
        int v;
        for (int i = cur[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].to;
            if (edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u])
            {
                flag = true;
                cur[u] = pre[v] = i;
                break;
            }
        }
        if (flag)
        {
            u = v;
            continue;
        }
        int Min = N;
        for (int i = head[u]; i != -1; i = edge[i].next)
            if (edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
            {
                Min = dep[edge[i].to];
                cur[u] = i;
            }
        gap[dep[u]]--;
        if (!gap[dep[u]])return ans;
        dep[u] = Min + 1;
        gap[dep[u]]++;
        if (u != start) u = edge[pre[u] ^ 1].to;
    }
    return ans;
}

int m, n;
int a, b, c;

int main()
{
    while (scanf("%d%d", &m, &n) != EOF)
    {
        init();
        while (m--)
        {
            scanf("%d%d%d", &a, &b, &c);
            addedge(a - 1, b - 1, c);
        }
        int ans = sap(0,n-1,n);
        printf("%d\n",ans);
    }
    return 0;
}