1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 int n,m; 7 const int maxn = 205; 8 int c[maxn][maxn],flow[maxn][maxn]; 9 int vis[maxn],fa[maxn]; 10 int largest_flow(int s,int t) 11 { 12 13 queue<int>q; 14 memset(flow,0,sizeof(flow)); 15 int ans = 0; 16 while(1) 17 { 18 memset(vis,0,sizeof(vis)); 19 q.push(s); 20 vis[s] = 0x3f3f3f3f; 21 while(!q.empty()){ 22 int u = q.front();q.pop(); 23 for(int v = 1;v<=n;++v)if(!vis[v]&&c[u][v]>flow[u][v]){ 24 fa[v] = u;q.push(v); 25 vis[v] = min(vis[u],c[u][v]-flow[u][v]); 26 } 27 } 28 // puts("--------------"); 29 if(vis[t]==0)break; 30 for(int i = t;i!=s;i = fa[i]){ 31 flow[fa[i]][i]+=vis[t]; 32 flow[i][fa[i]]-=vis[t]; 33 } 34 35 ans+=vis[t]; 36 } 37 return ans; 38 } 39 int main() 40 { 41 while(~scanf("%d%d",&m,&n)){ 42 memset(c,0,sizeof(c)); 43 for(int i = 1;i<=m;++i){ 44 int u,v,w;scanf("%d%d%d",&u,&v,&w); 45 c[u][v]+=w; 46 } 47 printf("%d\n",largest_flow(1,n)); 48 } 49 return 0; 50 }
时间: 2024-10-14 04:48:33