Drainage Ditches HDU - 1532
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.
Examples
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
Hint
题意:
网络里模板题, 源点为1, 汇点为N
题解:
学习一下FF和EK算法
FF算法是所有最大流算法的基础, 时间复杂度为(|F|E), 和流量的大小有关, 思路是增加反向边设立反悔的机制. 然后Dfs来求是否还能增广.
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const int inf = 1<<30;
const LL maxn = 210;
struct Edge{
int to, cap, rev;
Edge(int tt, int cc, int rr){to = tt, cap = cc, rev = rr;}
};
vector<Edge> es[maxn];
bool used[maxn];
int N, M;
int Dfs(int s, int e, int f){
if(s==e) return f;
used[s] = true;
for(int i = 0; i < (int)es[s].size(); i++){
Edge &v = es[s][i];
if(!used[v.to] && v.cap>0){
int d = Dfs(v.to, e, min(f, v.cap));
if(d > 0){
v.cap -= d;
es[v.to][v.rev].cap += d;
return d;
}
}
}
return 0;
}
LL maxFlow(){
LL flow = 0, f;
for(;;){
ms(used, 0);
f = Dfs(1, M, inf);
if(f == 0) return flow;
flow += f;
}
}
int main()
{
while(cin >> N >> M){
int to, from, cap;
ms(es, 0);
for(int i = 1; i <= N; i++){
cin >> from >> to >> cap;
es[from].push_back(Edge(to, cap, es[to].size()));
es[to].push_back(Edge(from, 0, es[from].size()-1));//反向边
}
cout << maxFlow() << endl;
}
return 0;
}
再来简单说一下EK算法, EK算法很大程度上是基于FF算法的, 只不过策略为每次都通过BFS找到最短的(边数)增广路
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const int inf = 1 << 30;
const LL maxn = 210;
int n, m; // 图中点的数目
int pre[maxn]; // 可行流中的前序节点
bool vis[maxn]; // 标记一个点是否被访问过
int mp[maxn][maxn]; // 记录图信息
bool bfs(int s, int t) {
queue <int> que;
memset(vis, 0, sizeof(vis));
memset(pre, -1, sizeof(pre));
pre[s] = s;
vis[s] = true;
que.push(s);
while(!que.empty()) {
int u = que.front();
que.pop();
for(int i = 1; i <= n; i++) {
if(mp[u][i] && !vis[i]) {
pre[i] = u;
vis[i] = true;
if(i == t)
return true;
que.push(i);
}
}
}
return false;
}
LL EK(int s, int t) {
LL ans = 0;
while(bfs(s, t)) { //bfs求得一条增广路
int mi = inf;
for(int i = t; i != s; i = pre[i]) {
mi = min(mi, mp[pre[i]][i]); //求出增广路中的流量
}
for(int i = t; i != s; i = pre[i]) { //正向边和反向边依次减去流量
mp[pre[i]][i] -= mi;
mp[i][pre[i]] += mi;
}
ans += mi;
}
return ans;
}
int main() {
int a, b, c;
while(cin >> m >> n){
ms(mp, 0);
while(m--){
cin >> a >> b >> c;
mp[a][b] += c; //考虑重边
}
cout << EK(1, n) << endl;
}
return 0;
}