您的位置:首页 > 理论基础 > 计算机网络

HDU1532 Drainage Ditches(网络流EdmondsKarp)

2016-08-17 14:58 513 查看
HDU1532 Drainage Ditches(网络流EdmondsKarp)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532

题目

Time Limit:1000MS     Memory Limit:32768KB


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

分析

网络流的EdmondsKarp算法水题,使用了刘汝佳的EdmondsKarp模板

源码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<sstream>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
#define mem0(x) memset(x,0,sizeof x)
#define mem1(x) memset(x,-1,sizeof x)
#define dbug cout<<"here"<<endl;
//#define LOCAL

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3+10;
const int MOD = 1000000007;

struct Edge{
int from, to, cap, flow;
Edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) {}
};

struct EdmondsKarp{
int n, m;
vector<Edge> edges;         //边数的两倍
vector<int> G[MAXN];        //邻接表,G[i][j]表示结点i的第j条边在e数组中的编号
int a[MAXN];                //当起点到i的可改进量
int p[MAXN];                //最短路树上p的入弧编号

void init(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);
}

int MaxFlow(int s, int t){
int flow = 0;
for( ; ; ){
mem0(a);
queue<int> Q;
while(!Q.empty())
Q.pop();
Q.push(s);
a[s] = INF;
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(!a[e.to] && e.cap>e.flow){
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap-e.flow);
Q.push(e.to);
}
}
if(a[t])
break;
}
if(!a[t])
break;
for(int u = t; u != s; u = edges[p[u]].from){
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
}
flow += a[t];
}
return flow;
}
};

EdmondsKarp graph;

int main(){
#ifdef LOCAL
freopen("C:\\Users\\asus-z\\Desktop\\input.txt","r",stdin);
freopen("C:\\Users\\asus-z\\Desktop\\output.txt","w",stdout);
#endif
int T;
int N, M;
int x,y,c;
scanf("%d", &T);
int kase = 0;
while(T--){
scanf("%d%d", &N, &M);
graph.init(N);
for(int i = 0; i < M; ++i){
scanf("%d%d%d", &x, &y, &c);
graph.AddEdge(x, y, c);
}
printf("Case %d: %d\n", ++kase, graph.MaxFlow(1, N));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: