Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 67110 Accepted: 25929
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
很基础的一个网络流题,直接套用算法入门经典网络流算法的模板,就能过。
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 300;
const int inf = 0x3f3f3f3f;
int g[N][N];
int flow[N][N];
int a[N];
int p[N];
int n,m;
int Edmonds_Karp(int s)
{
queue<int> Q;
memset(flow,0,sizeof(flow));
int f = 0,v,i;
while(1)
{
memset(a,0,sizeof(a));
a[s] = inf;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(v=1;v<=m;v++)
{
if(!a[v] && g[u][v]>flow[u][v])
{
p[v] = u;
Q.push(v);
a[v] = min(a[u],g[u][v]-flow[u][v]);
}
}
}
if(a[m]==0)
{
return f;
}
for(i=m;i!=s;i=p[i])
{
flow[p[i]][i] += a[m];
flow[i][p[i]] -= a[m];
}
f += a[m];
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int i,a,b,c;
memset(g,0,sizeof(g));
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&a,&b,&c);
g[a][b] += c;
}
printf("%d\n",Edmonds_Karp(1));
}
return 0;
}
Dinic
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 300;
int g[N][N];
int dist[N];
int n,m;
int BFS(int s)
{
memset(dist,-1,sizeof(dist));
queue<int> Q;
dist[s] = 0;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int v=1;v<=n;v++)
{
if(dist[v]<0 && g[u][v])
{
dist[v] = dist[u] + 1;
Q.push(v);
}
}
}
if(dist[n]>0)
{
return 1;
}
else
{
return 0;
}
}
int DFS(int x,int low)
{
int i,f = 0;
if(x==n)
{
return low;
}
for(i=1;i<=n;i++)
{
if(g[x][i] && dist[i]==dist[x] + 1)
{
f = DFS(i,min(low,g[x][i]));
if(f>0)
{
g[x][i] -= f;
g[i][x] += f;
return f;
}
}
}
return 0;
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(g,0,sizeof(g));
int i,j,x,y,z;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
g[x][y] += z;
}
int f = 0, t;
while(BFS(1))
{
while(t = DFS(1,inf))
{
f += t;
}
}
printf("%d\n",f);
}
return 0;
}