#include#include #include #include #include using namespace std;const int maxn=1e3+10,INF=1e8;struct Edge{ int from,to,cap,flow; Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}};struct Dinic{ int n,m,s,t; vector G[maxn]; vector edges; bool vis[maxn]; int d[maxn],cur[maxn]; void init(int n){ this->n=n; for(int i=0;i<=n;i++)G[i].clear(); edges.clear(); } void AddEdge(int from,int to,int cap){ edges.push_back(Edge(from,to,cap,0)); edges.push_back(Edge(to,from,0,0)); m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool bfs(){ memset(vis,false,sizeof(vis)); queue Q; vis[s]=true; d[s]=0; Q.push(s); while(!Q.empty()){ int x=Q.front();Q.pop(); for(int i=0;i e.flow){ vis[e.to]=1; d[e.to]=d[x]+1; Q.push(e.to); } } } return vis[t]; } int dfs(int x,int a){//a当前为止所有弧的最小残量 if(x==t || a==0)return a; int flow=0,f; for(int &i=cur[x];i 0){ e.flow+=f; edges[G[x][i]^1].flow-=f; flow+=f; a-=f; if(a==0)break; } } return flow; } int max_flow(int s,int t){ this->s=s;this->t=t; int flow=0; while(bfs()){ memset(cur,0,sizeof(cur)); flow+=dfs(s,INF); } return flow; }};Dinic solve;int main(){ int n,m; while(~scanf("%d%d",&m,&n)){ solve.init(n); while(m--){ int a,b,c; scanf("%d%d%d",&a,&b,&c); solve.AddEdge(a,b,c); } printf("%d\n",solve.max_flow(1,n)); } return 0;}/*7 61 2 101 3 102 4 42 5 83 5 94 6 105 6 10*/