Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 200 + 10;
const int INF = 99999999;
queue<int> apath;
int cap[maxn][maxn];
int flow[maxn][maxn];
int augPath[maxn];
int resNet[maxn];
void init()
{
while( !apath.empty() )
apath.pop();
memset( cap, 0, sizeof( cap ) );
memset( flow, 0, sizeof( flow ) );
}
int EdmondsKarp( int s, int t )
{
int maxFlow = 0;
while( true )
{
memset( resNet, 0, sizeof( resNet ) );
resNet[s] = INF;
augPath[s] = -1;
apath.push( s );
while( !apath.empty() )
{
int cur = apath.front();
apath.pop();
for( int i = 1; i <= t; i++ )
{
if( !resNet[i] && flow[cur][i] < cap[cur][i] )
{
apath.push( i );
augPath[i] = cur;
resNet[i] = min( cap[cur][i] - flow[cur][i], resNet[cur] );
}
}
}
if( resNet[t] == 0 )
break;
for( int k = t; augPath[k] != -1; k = augPath[k] )
{
flow[augPath[k]][k] += resNet[t];
flow[k][augPath[k]] -= resNet[t];
}
maxFlow += resNet[t];
}
return maxFlow;
}
int main()
{
int n;
int m;
int si, ei, ci;
while( ~scanf( "%d%d", &n, &m ) )
{
init();
while( n-- )
{
scanf( "%d%d%d", &si, &ei, &ci );
cap[si][ei] += ci;
}
printf( "%d\n", EdmondsKarp( 1, m ) );
}
return 0;
}