网络流裸题, ek
#include <stdio.h>
#include <string.h>
#include <queue>
#define MAXSIZE 201
#define INF 0x7fffffff
using namespace std;
int edge;
int vertex;
int level[MAXSIZE];
int r[MAXSIZE][MAXSIZE];
bool bfs(void)
{
int cur;
queue<int> q;
memset(level, -1, sizeof(level));
q.push(1);
level[1] = 0;
while( !q.empty() )
{
cur = q.front();
q.pop();
for(int i = 1; i <= vertex; i ++)
{
if( -1 != level[i] || !r[cur][i] )
continue;
level[i] = level[cur]+1;
q.push(i);
}
}
return (-1 == level[vertex])? false : true;
}
int dfs(int u, int cur_val)
{
int tmp_val;
int val;
if( u == vertex )
return cur_val;
tmp_val = cur_val;
for(int i = 1; i <= vertex; i ++)
{
if( (level[i] != level[u]+1) || !r[u][i] )
continue;
val = dfs(i, min(tmp_val, r[u][i]));
tmp_val -= val;
r[u][i] -= val;
r[i][u] += val;
}
return cur_val - tmp_val;
}
int dinic_algorithm(void)
{
int tmp_val;
int ans;
ans = tmp_val = 0;
while( true == bfs() )
while( tmp_val = dfs(1, INF) )
ans += tmp_val;
return ans;
}
int main(int argc, char const *argv[])
{
//freopen("test.in", "r", stdin);
int from;
int to;
int val;
while( ~scanf("%d %d", &edge, &vertex) )
{
memset(r, 0, sizeof(r));
for(int i = 0; i < edge; i ++)
{
scanf("%d %d %d", &from, &to, &val);
r[from][to] += val;
}
printf("%d\n", dinic_algorithm());
}
return 0;
}