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
题解
这道题是一道裸的最大流,没什么好说的
不过这里有一个坑
每次加边的head数组要初始化为-1,自己以前都是0,被坑了
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#define N 205
#define MAX 1e8
using namespace std;
int n,m,x,y,z,tot,ans,fee,Min;
int head[N],level[N];
struct node{
int next,to,fee;
}e[*N];
void add(int x,int y,int z){
e[tot].next=head[x];
head[x]=tot;
e[tot].to=y;
e[tot].fee=z;
tot++;
e[tot].next=head[y];
head[y]=tot;
e[tot].to=x;
e[tot].fee=;
tot++;
}
queue<int> q;
bool bfs(int s,int t){
memset(level,,sizeof(level));
level[s]=;
while (!q.empty()) q.pop();
q.push(s);
while (!q.empty()){
int k=q.front();
q.pop();
if (k==t) return true;
for (int i=head[k];i!=-;i=e[i].next){
int v=e[i].to;
if (e[i].fee&&!level[v]){
level[v]=level[k]+;
q.push(v);
}
}
}
return false;
}
int dfs(int s,int maxf,int t){
if (s==t) return maxf;
int ret=;
for (int i=head[s];i!=-;i=e[i].next){
int v=e[i].to;
fee=e[i].fee;
if (level[v]==level[s]+){
Min=min(maxf-ret,fee);
fee=dfs(v,Min,t);
e[i].fee-=fee;
e[i^].fee+=fee;
ret+=fee;
if (ret==maxf) return ret;
}
}
return ret;
}
int Dinic(int s,int t){
ans=;
while (bfs(s,t)) ans+=dfs(s,MAX,t);
return ans;
}
int main(){
while (~scanf("%d%d",&n,&m)){
tot=;
memset(head,-,sizeof(head));
for (int i=;i<=n;i++)
scanf("%d%d%d",&x,&y,&z),add(x,y,z);
printf("%d\n",Dinic(,m));
}
return ;
}
这是之前做的
现在发现Dinic有一个不错的优化
就是在dfs找答案的时候判断答案是否为0,为0的话就说明当前这个点到达不了汇点,那么直接把level改为0,这样可以减少很多重复的操作
因为有可能很多的层次网络都是经过s的,那么把s的level改掉后就有很多不用做了
其实就多了一句话而已
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#define N 205
#define MAX 1e8
using namespace std;
int n,m,x,y,z,tot,ans,fee,Min;
int head[N],level[N];
struct node{
int next,to,fee;
}e[*N];
void add(int x,int y,int z){
e[tot].next=head[x];
head[x]=tot;
e[tot].to=y;
e[tot].fee=z;
tot++;
e[tot].next=head[y];
head[y]=tot;
e[tot].to=x;
e[tot].fee=;
tot++;
}
queue<int> q;
bool bfs(int s,int t){
memset(level,,sizeof(level));
level[s]=;
while (!q.empty()) q.pop();
q.push(s);
while (!q.empty()){
int k=q.front();
q.pop();
if (k==t) return true;
for (int i=head[k];i!=-;i=e[i].next){
int v=e[i].to;
if (e[i].fee&&!level[v]){
level[v]=level[k]+;
q.push(v);
}
}
}
return false;
}
int dfs(int s,int maxf,int t){
if (s==t) return maxf;
int ret=;
for (int i=head[s];i!=-;i=e[i].next){
int v=e[i].to;
fee=e[i].fee;
if (level[v]==level[s]+){
Min=min(maxf-ret,fee);
fee=dfs(v,Min,t);
e[i].fee-=fee;
e[i^].fee+=fee;
ret+=fee;
if (ret==maxf) return ret;
}
}
if (!ret) level[s]=; //这里是关键
return ret;
}
int Dinic(int s,int t){
ans=;
while (bfs(s,t)) ans+=dfs(s,MAX,t);
return ans;
}
int main(){
while (~scanf("%d%d",&n,&m)){
tot=;
memset(head,-,sizeof(head));
for (int i=;i<=n;i++)
scanf("%d%d%d",&x,&y,&z),add(x,y,z);
printf("%d\n",Dinic(,m));
}
return ;
}