poj 1273 Drainage Ditches

http://poj.org/problem?id=1273
#include
<iostream> #include<cmath> #include<string> #include<algorithm> #include<queue> #include<cstring> #include<cstdio> using namespace std; const int MAX=2000000005; const int N=205; int h[N]; int flow[N][N]; int Bfs(int n) { memset(h,0,sizeof(h)); queue<int>str; str.push(1); h[1]=1; while(!str.empty()) { int k=str.front(); str.pop(); for(int i=1;i<=n;++i) { if(h[i]==0&&flow[k][i]>0) { h[i]=h[k]+1; str.push(i); } } } return h[n]; } int end; int Add(int x,int M) { if(x==end) return M; int sum=M; for(int i=1;i<=end;++i) { if(flow[x][i]>0&&h[i]==h[x]+1) { int w=Add(i,min(sum,flow[x][i])); sum=sum-w; flow[x][i]-=w; flow[i][x]+=w; if(sum==0) break; } } return (M-sum); } int main() { int n,m; while(cin>>n>>m) { end=m; memset(flow,0,sizeof(flow)); while(n--) { int i,j,k; cin>>i>>j>>k; flow[i][j]+=k; } int ans=0; while(Bfs(m)) { ans+=Add(1,MAX); } cout<<ans<<endl; } return 0; }

你可能感兴趣的:(poj)