果的网络流,有重边
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N=210;
int n, m;
int mat[N][N];
int EKarp(int s, int t)
{
queue < int > que;
int flow[N][N], pre[N], temp[N], u, v, sum= 0;
memset(flow, 0 , sizeof(flow));
while (1)
{
que.push(s);
memset(temp, 0, sizeof(temp));
temp[s]= 0x7fffffff;
while ( ! que.empty() )
{
u= que.front();
que.pop();
for ( v= 1 ; v <= n ; v++ )
if ( !temp[v] && mat[u][v] > flow[u][v] )
{
que.push(v);
temp[v]= temp[u] < mat[u][v]-flow[u][v] ? temp[u] : mat[u][v]-flow[u][v];
pre[v]= u;
}
}
if ( temp[t] == 0 )
break;
for ( u = t ; u != s ; u=pre[u] )
{
flow[pre[u]][u] += temp[t];
flow[u][pre[u]] -= temp[t];
}
sum+= temp[t];
}
return sum;
}
int main()
{
while ( EOF != scanf("%d%d", &m, &n) )
{
int i, j, k, t;
memset(mat, 0, sizeof(mat));
for ( k = 0 ; k < m ; k++ )
{
scanf("%d%d%d", &i, &j, &t);
mat[i][j] += t;
}
printf("%d\n", EKarp(1, n));
}
return 0;
}