Pagini recente » Cod sursa (job #2113452) | Cod sursa (job #848668) | Cod sursa (job #1724621) | Cod sursa (job #2968702) | Cod sursa (job #2682470)
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;
const int NMAX = 1000;
const int INF = 2.e9;
int c[NMAX + 5][NMAX + 5], f[NMAX + 5][NMAX + 5], t[NMAX + 5], s, d;
vector <int> G[NMAX + 5];
int bfs()
{
int u, v, i;
queue <int> q;
q.push(s);
memset(t, 0, sizeof(t));
t[s] = -1;
while(!q.empty())
{
u = q.front();
q.pop();
if(u == d)
continue;
for(i = 0 ; i < G[u].size() ; i ++)
{
v = G[u][i];
if(c[u][v] - f[u][v] > 0 && !t[v])
{
t[v] = u;
q.push(v);
}
}
}
return (t[d] != 0);
}
int update_flow()
{
int nod, flow;
flow = INF;
for(nod = d ; t[nod] != -1 ; nod = t[nod])
flow = min(flow, c[t[nod]][nod] - f[t[nod]][nod]);
for(nod = d ; t[nod] != -1 ; nod = t[nod])
{
f[t[nod]][nod] += flow;
f[nod][t[nod]] -= flow;
}
return flow;
}
int max_flow()
{
int flow = 0, nod , i;
while(bfs())
for(i = 0 ; i < G[d].size() ; i ++)
{
nod = G[d][i];
if(c[nod][d] - f[nod][d] == 0 || !t[nod])
continue;
t[d] = nod;
flow += update_flow();
}
return flow;
}
int main()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int n, m, i, x, y, z;
scanf("%d%d", &n, &m);
for(i = 1 ; i <= m ; i ++)
{
scanf("%d%d%d", &x, &y, &z);
G[x].push_back(y);
G[y].push_back(x);
c[x][y] = z;
}
s = 1;
d = n;
printf("%d\n" , max_flow());
return 0;
}