# [USACO4.2]草地排水Drainage Ditches

## 题目分析

``` 1 #include<bits/stdc++.h>
2 using namespace std;
3 const int maxn = 1e6+35;
4 const int INF = 2e9;
5 struct Edge
6 {
7     int from, to, cap, flow;
8     Edge(int u, int v, int c, int f):from(u),to(v),cap(c),flow(f) {}
9 };
10 struct EdmondsKarp
11 {
12     int n,m;
13     vector<Edge> edges;
14     vector<int> G[maxn];
15     int a[maxn],p[maxn];
16
17     void init(int n)
18     {
19         for (int i=0; i<=n; i++)G[i].clear();
20         edges.clear();
21     }
22
23     void add_edge(int from, int to, int cap)
24     {
25         edges.push_back(Edge(from, to, cap, 0));
26         edges.push_back(Edge(to, from, 0, 0));
27         int mx = edges.size();
28         G[from].push_back(mx-2);
29         G[to].push_back(mx-1);
30     }
31
32     long long Maxflow(int s, int t)
33     {
34         long long flow = 0;
35         for (;;)
36         {
37             memset(a, 0, sizeof(a));
38             queue<int> Q;
39             Q.push(s);
40             a[s] = INF;
41             while (Q.size())
42             {
43                 int x = Q.front();Q.pop();
44                 for (int i=0; i<G[x].size(); i++)
45                 {
46                     Edge& e = edges[G[x][i]];
47                     if (!a[e.to] && e.cap>e.flow)
48                     {
49                         p[e.to] = G[x][i];
50                         a[e.to] = min(a[x], e.cap-e.flow);
51                         Q.push(e.to);
52                     }
53                 }
54                 if (a[t])break;
55             }
56             if (!a[t])break;
57             for (int u=t; u!=s; u = edges[p[u]].from)
58             {
59                 edges[p[u]].flow += a[t];
60                 edges[p[u]^1].flow -= a[t];
61             }
62             flow += a[t];
63         }
64         return flow;
65     }
66 }f;
68 {
69     char ch=getchar();int num = 0;
70     while (ch<‘0‘||ch>‘9‘)ch = getchar();
71     while (ch>=‘0‘&&ch<=‘9‘){num=num*10+ch-‘0‘;ch = getchar();}
72     return num;
73 }
74 int main()
75 {
76     scanf("%d%d",&f.m,&f.n);
77     f.init(f.n);
78     for (int i=1; i<=f.m; i++)
79     {
82     }
83     printf("%lld\n",f.Maxflow(1, f.n));
84 }```

1.Cptraser：www.cnblogs.com/Cptraser/p/7921455.html

[USACO4.2]草地排水Drainage Ditches

(0)
(0)

0条