最大流 POJ1273 Drainage Ditches
题目描述: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.
输入: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.
输出:For each case, output a single integer, the maximum rate at which water may emptied from the pond.
样例:
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10 输出50
分析:很明显这道题就是求网络容量的最大流,但是要小心重边!数据很小,采用一般增广路即可
代码如下:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 205;
const int MAXM = 205;
struct Edge{
int f,c;
};
int n,m;
Edge edge[MAXN][MAXN];
int flag[MAXN],pre[MAXN],alpha[MAXN];
queue<int > q;
void init(){
memset(edge,0x3f,sizeof(edge));
int u,v,c;
for(int i = 0;i < m;i++) {
scanf("%d%d%d",&u,&v,&c);
if(edge[u-1][v-1].c == INF) {
edge[u-1][v-1].c = c;
edge[u-1][v-1].f = 0;
}
else edge[u-1][v-1].c += c;//重边
}
}
void ford() {
for(;;) {
memset(flag,0xff,sizeof(flag));
memset(pre,0xff,sizeof(pre));
flag[0] = 0;pre[0] = 0;alpha[0] = INF;
while(!q.empty()) q.pop();
q.push(0);
while(!q.empty() && flag[n-1] == -1) {
int v = q.front();q.pop();
for(int i = 0;i < n;i++) {
if(flag[i] == -1) {
if(edge[v][i].c < INF && edge[v][i].f < edge[v][i].c) {
flag[i] = 0;pre[i] = v;
alpha[i] = min(alpha[v],edge[v][i].c - edge[v][i].f);
q.push(i);
}
else if(edge[i][v].c < INF && edge[i][v].f > 0) {
flag[i] = 0;pre[i] = -v;
alpha[i] = min(alpha[v],edge[i][v].f);
q.push(i);
}
}
}
flag[v] = 1;
}
if(flag[n-1] == -1 || alpha[n-1] == 0) break;
int k1 = n-1,k2 = abs(pre[k1]);
int a = alpha[n-1];
while(1) {
if(edge[k2][k1].f < INF) edge[k2][k1].f += a;
else edge[k1][k2].f-=a;
if(k2 == 0) break;
k1 = k2;k2 = abs(pre[k2]);
}
}
int maxflow = 0;
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
if(i == 0 && edge[i][j].f != INF) maxflow+=edge[i][j].f;
}
}
printf("%d\n",maxflow);
/*for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
if(i!=j) printf("%d %d\n",edge[i][j].c,edge[i][j].f);
}
}*/
}
int main() {
while(scanf("%d%d",&m,&n) != EOF) {
init();
ford();
}
}