HDU-1532
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<bits/stdc++.h> #define FIN freopen("input.txt","r",stdin) #define ll long long #define mod 1000000007 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define inf 2e9 const int maxn = 405; using namespace std; int cnt,n,m; int head[maxn],Next[maxn*200],to[maxn*200]; int flow[maxn*200],dep[maxn],cur[maxn]; inline void add(int u,int v,int w){ Next[cnt]=head[u]; head[u]=cnt; to[cnt]=v; flow[cnt++]=w; } int dfs(int u,int cost){ if(u==m) return cost; for(int i=head[u];i!=-1;i=Next[i]){ if(dep[to[i]]==dep[u]+1&&flow[i]>0){ int dis=dfs(to[i],min(flow[i],cost)); if(dis>0){ flow[i]-=dis; flow[i^1]+=dis; return dis; } } } return 0; } int bfs(){ queue<int> q; q.push(1); memset(dep,0,sizeof(dep)); dep[1]=1; while(!q.empty()){ int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=Next[i]){ if(flow[i]>0&&dep[to[i]]==0){ dep[to[i]]=dep[u]+1; q.push(to[i]); } } } if(dep[m]>0) return 1; return 0; } int dicnic(){ int ans=0; while(bfs()){ int cost; for(int i=1;i<=m+2;i++){ cur[i]=head[i]; } while(cost=dfs(1,inf)) ans+=cost; } return ans; } void init(){ memset(head,-1,sizeof(head)); cnt; } int main() { #ifndef ONLINE_JUDGE FIN; #endif while(~scanf("%d %d",&n,&m)){ init(); for(int i=1;i<=n;i++){ int u,v,w; scanf("%d %d %d",&u,&v,&w); add(u,v,w); add(v,u,0); } printf("%d\n",dicnic()); } }