提交地址
Drainage Ditches
Time Limit 1000 ms
Memory Limit 65536 kb
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. Note however,
that there can be more than one ditch between two intersections.
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
Input file contains multiple test cases.
In a test case:
Line 1: 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.
Line 2..N+1: Each of 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,One line with 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
source
USACO 4.2
题意:就是给出各个边的最大流量,和起点终点,求最大流。
Code (Edmonds-Karp算法):
#include <cstdio> #include <cstring> #include <queue> #define MAXN 205 #define INF 0x7fffffff using namespace std; int cap[MAXN][MAXN], f[MAXN][MAXN]; int pre[MAXN];//记录增广路径 int p[MAXN];//记录增广时的残量 int m, n; int Edmonds_Karp(int s, int t) { int v, u; int ans = 0; queue<int> q; memset(f, 0, sizeof(f)); while (true) {//BFS找增广路 memset(p, 0, sizeof(p)); p[s] = INF; q.push(s); while (!q.empty()) { u = q.front(); q.pop(); for (v = 1; v <= n; v++) if (!p[v] && cap[u][v] > f[u][v]) { //找到新节点 pre[v] = u;//记录v的父亲,并加入FIFO队列 q.push(v); p[v] = p[u] < cap[u][v] - f[u][v] ? p[u] : cap[u][v] - f[u][v]; //s-v路径上的最小残量 } } if (p[t] == 0) break;//找不到增广路,则当前流已经是最大流::最小割最大流定理 for (u = t; u != s; u = pre[u]) { f[pre[u]][u] += p[t]; //更新正向流量 f[u][pre[u]] -= p[t]; //更新反向流量 } ans += p[t]; //更新从s流出的总流量 } return ans; } int main() { int i, a, b, c; while (~scanf("%d%d", &m, &n)) { memset(cap, 0, sizeof(cap)); for (i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); cap[a][b] += c; } printf("%d\n", Edmonds_Karp(1, n)); } return 0; }
Code(Dinic ):
#include <cstdio> #include <cstring> #include <queue> #define MAXN 205 #define INF 1000000000 using namespace std; struct Edge { int from, to, cap, flow; }; struct Dinic { int n, m, s, t; vector<Edge> edges; //边表.edges[e]和edges[e^1]互为反向弧 vector<int> G[MAXN]; //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号 bool vis[MAXN]; //BFS使用 int d[MAXN]; //从起点到i的距离 int cur[MAXN]; //当前弧指针 void ClearAll(int n) { for (int i = 0; i < n; i++) G[i].clear(); edges.clear(); } void AddEdge(int from, int to, int cap) { edges.push_back((Edge) {from, to, cap, 0}); edges.push_back((Edge) {to, from, 0, 0}); m = edges.size(); G[from].push_back(m - 2); G[to].push_back(m - 1); } bool BFS() {//使用BFS计算出每一个点在残量网络中到t的最短距离d. memset(vis, 0, sizeof(vis)); queue<int> Q; Q.push(s); vis[s] = 1; d[s] = 0; while (!Q.empty()) { int x = Q.front(); Q.pop(); for (int i = 0; i < G[x].size(); i++) { Edge& e = edges[G[x][i]]; if (!vis[e.to] && e.cap > e.flow) { //只考虑残量网络中的弧 vis[e.to] = 1; d[e.to] = d[x] + 1; Q.push(e.to); } } } return vis[t]; } int DFS(int x, int a) {//使用DFS从S出发,沿着d值严格递减的顺序进行多路增广。 if (x == t || a == 0) return a; int flow = 0, f; for (int& i = cur[x]; i < G[x].size(); i++) { Edge& e = edges[G[x][i]]; if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) { e.flow += f; edges[G[x][i] ^ 1].flow -= f; flow += f; a -= f; if (a == 0) break; } } return flow; } int Maxflow(int s, int t) { this->s = s; this->t = t; int flow = 0; while (BFS()) { memset(cur, 0, sizeof(cur)); flow += DFS(s, INF); } return flow; } }; Dinic g; int main() { int n, m, i, a, b, c; while (~scanf("%d%d", &m, &n)) { g.ClearAll(n + 1); for (i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); g.AddEdge(a, b, c); } int flow = g.Maxflow(1, n); printf("%d\n", flow); } return 0; }