Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15749 Accepted Submission(s): 7502
Problem 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.
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
Source
Recommend
lwg
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532
最大流入门题。参考资料:http://blog.youkuaiyun.com/hurmishine/article/details/53037362
用了数组和Vector建图,对于初学者来说,先看懂数组建图的,再去研究vector建图的。
AC代码1:数组建图:
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.youkuaiyun.com/hurmishine
*
*/
#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
const int msize = 205;
int n, m; // m–路径数, n–结点数
int a[msize][msize]; //
int pre[msize]; // 记录结点i的前向结点为pre[i]
bool vis[msize]; // 记录结点i是否已访问
// 用BFS来判断从结点s到t的路径上是否还有delta
// 即判断s,t之间是否还有增广路径,若有,返回1
bool BFS(int s, int t)
{
queue<int> q;
memset(pre, -1, sizeof(pre));
memset(vis, false, sizeof(vis));
pre[s] = s;
vis[s] = true;
q.push(s);
int p;
while(!q.empty())
{
p = q.front();
q.pop();
for(int i=1; i<=n; ++i)
{
if(a[p][i]>0 && !vis[i])
{
pre[i] = p;
vis[i] = true;
if(i == t) // 存在增广路径
return true;
q.push(i);
}
}
}
return false;
}
int EK(int s, int t)
{
int maxflow = 0, d;
while(BFS(s, t))
{
d= INT_MAX;
// 若有增广路径,则找出最小的delta
for(int i=t; i!=s; i=pre[i])
d = min(d, a[pre[i]][i]);
// 这里是反向边,看讲解
for(int i=t; i!=s; i=pre[i])
{
a[pre[i]][i] -= d;
a[i][pre[i]] += d;
}
maxflow += d;
}
return maxflow;
}
int main()
{
//freopen("data.txt","r",stdin);
while(cin >> m >> n)
{
memset(a, 0, sizeof(a));
int x,y,z;
for(int i=0; i<m; ++i)
{
cin >> x >> y >> z;
a[x][y] += z; // 有重边时则加上z
}
cout << EK(1, n) << endl;
}
return 0;
}
AC代码2:Vector建图:
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.youkuaiyun.com/hurmishine
*
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=200+5;
const int INF=1e9;
int n,m;
struct Edge
{
int u,v,cap,flow;
Edge(){}
Edge(int u,int v,int cap,int flow):u(u),v(v),cap(cap),flow(flow) {}
};
vector<Edge>edge;
vector<int>G[maxn];
int a[maxn];
int p[maxn];
void Init()
{
for(int i=0; i<n; i++)
G[i].clear();
edge.clear();
}
void addEdge(int u,int v,int cap)
{
edge.push_back(Edge(u,v,cap,0));
edge.push_back(Edge(v,u,0,0));
int s=edge.size();
G[u].push_back(s-2);
G[v].push_back(s-1);
}
//EdmondsKarp
int maxnFlow(int s,int t)
{
int flow=0;
for(;;)
{
memset(a,0,sizeof(a));
queue<int>q;
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=edge[G[x][i]];
if(!a[e.v]&&e.cap>e.flow)
{
p[e.v]=G[x][i];
a[e.v]=min(a[x],e.cap-e.flow);
q.push(e.v);
}
}
if(a[t]) break;
}
if(!a[t]) break;
for(int u=t;u!=s;u=edge[p[u]].u)
{
edge[p[u]].flow+=a[t];
edge[p[u]^1].flow-=a[t];
}
flow+=a[t];
}
return flow;
}
int main()
{
//freopen("data.txt","r",stdin);
while(cin>>m>>n)
{
int x,y,z;
Init();
while(m--)
{
cin>>x>>y>>z;
addEdge(x,y,z);
}
cout<<maxnFlow(1,n)<<endl;
}
return 0;
}