Cod sursa(job #240785)
#include <cstdio>
#include <vector>
using namespace std;
#define max 1005
#define inf 0x3f3f3f
int n, m;
int c[max][max], f[max][max], t[max];
bool viz[max];
vector <int> v[max];
void citire()
{
scanf("%d %d", &n, &m);
int x, y, z;
for (int i=1; i<=m; ++i)
{
scanf("%d %d %d",&x, &y, &z);
v[x].push_back(y);
v[y].push_back(x);
c[x][y]=z;
}
}
bool see()
{
int q[max], nod;
q[0]=q[1]=1;
memset(viz,0, sizeof viz);
viz[1]=1;
for (int i=1; i<=q[0]; ++i)
{
nod = q[i];
if (nod == n) continue;
for (int i=0; i<v[nod].size(); ++i)
{
int k=v[nod][i];
if (f[nod][k]==c[nod][k] || viz[k]) continue;
viz[k]=1;
q[++q[0]]=k;
t[k]=nod;
}
}
return viz[n];
}
void flux()
{
int flow=0, fmin;
while (see())
for (int i=0; i<v[n].size(); ++i)
{
int nod=v[n][i];
if (f[nod][n]==c[nod][n] || viz[nod]==0) continue;
t[n] = nod;
fmin = inf;
for (int i=n; i!=1; i=t[i])
fmin=min(fmin, c[t[i]][i] - f[t[i]][i]);
for (int i=n; i!=1; i=t[i])
f[t[i]][i] += fmin,
f[i][t[i]] -= fmin;
flow += fmin;
}
printf("%d\n",flow);
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
citire();
flux();
}