描述
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.
n为边数,m为点数,s,e之间有容量为c的边,求最大流。
输出
对于每组输入输出最大流(整数)
样例输入
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
样例输出
50
我的程序...AC...
#include<stdio.h>
#include<stdlib.h>
#define MAXN 201
int g[MAXN][MAXN];
int f[MAXN][MAXN];
int q[MAXN],qs,qe;
int fa[MAXN];
int fl[MAXN];
int n,m;
int ek(void);
int main(void)
{
int i,s,e,c;
while(~scanf("%d%d",&n,&m))
{
memset(g,0x0,sizeof(g));
memset(f,0x0,sizeof(f));
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&s,&e,&c);
g[s][e]+=c;
}
printf("%d\n",ek());
}
return 0;
}
int ek(void)
{
int i,s,maxflow=0;
while(1){
memset(fa,0x0,sizeof(fa));
memset(fl,0x0,sizeof(fl));
qs=0;qe=0;
q[0]=1;
fl[1]=0x7fffffff;
while(~(qe-qs))
{
s=q[qs++];
for(i=1;i<=m;i++)
{
if((!fa[i])&&g[s][i]>f[s][i])
{
fa[i]=s;
fl[i]=fl[s]<g[s][i]-f[s][i]?fl[s]:g[s][i]-f[s][i];
q[++qe]=i;
if(s==m) goto out;
}
}
}
out:
if(fl[m]==0) break;
for(i=m;i!=1;i=fa[i])
{
f[fa[i]][i]+=fl[m];
f[i][fa[i]]-=fl[m];
}
maxflow+=fl[m];
}
return maxflow;
}
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.
n为边数,m为点数,s,e之间有容量为c的边,求最大流。
输出
对于每组输入输出最大流(整数)
样例输入
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
样例输出
50
我的程序...AC...
#include<stdio.h>
#include<stdlib.h>
#define MAXN 201
int g[MAXN][MAXN];
int f[MAXN][MAXN];
int q[MAXN],qs,qe;
int fa[MAXN];
int fl[MAXN];
int n,m;
int ek(void);
int main(void)
{
int i,s,e,c;
while(~scanf("%d%d",&n,&m))
{
memset(g,0x0,sizeof(g));
memset(f,0x0,sizeof(f));
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&s,&e,&c);
g[s][e]+=c;
}
printf("%d\n",ek());
}
return 0;
}
int ek(void)
{
int i,s,maxflow=0;
while(1){
memset(fa,0x0,sizeof(fa));
memset(fl,0x0,sizeof(fl));
qs=0;qe=0;
q[0]=1;
fl[1]=0x7fffffff;
while(~(qe-qs))
{
s=q[qs++];
for(i=1;i<=m;i++)
{
if((!fa[i])&&g[s][i]>f[s][i])
{
fa[i]=s;
fl[i]=fl[s]<g[s][i]-f[s][i]?fl[s]:g[s][i]-f[s][i];
q[++qe]=i;
if(s==m) goto out;
}
}
}
out:
if(fl[m]==0) break;
for(i=m;i!=1;i=fa[i])
{
f[fa[i]][i]+=fl[m];
f[i][fa[i]]-=fl[m];
}
maxflow+=fl[m];
}
return maxflow;
}