模板题
/*
ID:jinbo wu
TASK:ditch
LANG:C++
*/
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int n,m;
struct node
{
int c,f;
}e[250][250];
queue<int> q;
int level[250];
bool dinic_bfs()
{
memset(level,0,sizeof(level));
q.push(1);
level[1]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=1;i<=m;i++)
{
if(!level[i]&&e[u][i].c>e[u][i].f)
{
level[i]=level[u]+1;
q.push(i);
}
}
}
return level[m]!=0;
}
int dinic_dfs(int u,int cp)
{
int t;
int tmp=cp;
if(u==m)
return cp;
for(int i=1;i<=m&&tmp;i++)
{
if(level[i]==level[u]+1)
{
t=dinic_dfs(i,min(tmp,e[u][i].c-e[u][i].f));
e[u][i].f+=t;
e[i][u].f-=t;
tmp-=t;
}
}
return cp-tmp;
}
int dinic()
{
int sum=0;
int tf;
while(dinic_bfs())
{
while(tf=dinic_dfs(1,INF))
{
sum+=tf;
}
}
return sum;
}
int main()
{
freopen("ditch.in","r",stdin);
freopen("ditch.out","w",stdout);
cin>>n>>m;
int a,b,c;
for(int i=1;i<=n;i++)
{
cin>>a>>b>>c;
e[a][b].c+=c;
}
cout<<dinic()<<endl;
}