#include <iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; #define INF 0xfffffff #define N 210 int cap[N][N],flow[N][N]; int pre[N],dist[N]; int ek(int sta,int end){ int i,curr,sum=0; memset(flow,0,sizeof(flow)); while(1){ memset(pre,0,sizeof(pre)); memset(dist,0,sizeof(dist)); queue<int> q; q.push(sta); dist[sta]=INF; pre[sta]=sta; while(!q.empty()){ curr=q.front(); q.pop(); for(i=1;i<=end;i++) if(!dist[i]&&flow[curr][i]<cap[curr][i]){ dist[i]=min(dist[curr],cap[curr][i]-flow[curr][i]); pre[i]=curr; q.push(i); } } if(dist[end]==0) break; for(i=end;i!=sta;i=pre[i]){ curr=pre[i]; flow[curr][i]+=dist[end]; flow[i][curr]-=dist[end]; } sum+=dist[end]; } return sum; } int main(int argc, char** argv) { int n,m,i,a,b,w; while(scanf("%d%d",&n,&m)!=EOF){ memset(cap,0,sizeof(cap)); for(i=1;i<=n;i++){ scanf("%d%d%d",&a,&b,&w); cap[a][b]+=w; } printf("%d\n",ek(1,m)); } return 0; } #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define N 210 #define INF 0xfffffff int flow[N][N],dist[N],m,n; int bfs(){ memset(dist,-1,sizeof(dist)); dist[1]=0; queue<int> q; q.push(1); while(!q.empty()){ int k=q.front(); q.pop(); for(int i=1;i<=n;i++){ if(flow[k][i]>0&&dist[i]<0){ dist[i]=dist[k]+1; q.push(i); } } } if(dist[n]>0) return 1; else return 0; } int dfs(int x,int mx){ int i,a; if(x==n) return mx; for(i=1;i<=n;i++){ if(flow[x][i]>0&&dist[i]==dist[x]+1&&(a=dfs(i,min(mx,flow[x][i])))){ flow[x][i]-=a; flow[i][x]+=a; return a; } } return 0; } int main(){ int i,b,a,w,ans,res; while(scanf("%d%d",&m,&n)!=EOF){ memset(flow,0,sizeof(flow)); for(i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&w); flow[a][b]+=w; } ans=0; while(bfs()) while(res=dfs(1,INF)) ans+=res; printf("%d\n",ans); } }