【Poj1273】Drainage Ditches(網絡流)
阿新 • • 發佈:2018-01-14
ace ring tmp scanf down std ems mar scan
Description
給圖,求最大流
最大流模板題,這裏用dinic
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Inf 0x7fffffff
#define N 210
using namespace std;
int g[N][N],d[N],q[N*10],h,t;
int n,m,Ans,tmp;
bool Bfs(){
memset(d,-1,sizeof(d));
d[1]=0;
h=0,t=1;
q[1]=1;
while (h<t){
int u=q[++h];
for(int v=1;v<=n;++v)
if(d[v]<0&&g[u][v]>0){
d[v]=d[u]+1;
q[++t]=v;
}
}
return d[n]>0;
}
int dfs(int u,int low){
if(u==n) return low;
int tmp;
for(int v=1;v<=n;++v){
if (g[u][v]>0&&d[v]==d[u]+1&&(tmp=dfs(v,min(low,g[u][v])))){
g[u][v]-=tmp;
g[v][u]+=tmp;
return tmp;
}
}
return 0;
}
int main(){
while(~scanf("%d%d",&m,&n)){
memset(g,0,sizeof(g));
for(int i=1,u,v,f;i<=m;++i){
scanf(" %d%d%d",&u,&v,&f);
g[u][v]+=f;
}
Ans=0;
while(Bfs()) {
while(tmp=dfs(1,Inf)) Ans+=tmp;
}
printf("%d\n",Ans);
}
return 0;
}
【Poj1273】Drainage Ditches(網絡流)