Pagini recente » Cod sursa (job #392170) | Cod sursa (job #1207530) | Cod sursa (job #28599) | Cod sursa (job #2972232) | Cod sursa (job #780754)
Cod sursa(job #780754)
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define nmax 1010
#define INF 0x3f3f3f3f;
vector<int> G[nmax];
int capacity[nmax][nmax], currentFlow[nmax][nmax], father[nmax];
int N, M, X, Y, C, maxFlow, minFlow;
bool used[nmax];
bool check()
{
memset(used, false, sizeof(used));
queue<int> Q;
used[1] = true;
Q.push(1);
while(!Q.empty())
{
int node = Q.front();
Q.pop();
if(node == N) break;
vector<int> :: iterator it;
for(it = G[node].begin(); it != G[node].end(); ++it)
if(!used[*it] && capacity[node][*it] > currentFlow[node][*it])
{
used[*it] = 1;
Q.push(*it);
father[*it] = node;
}
}
return used[N];
}
int main()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int i, j;
vector<int> :: iterator it;
scanf("%i %i", &N, &M);
for(i = 1; i <= M; i++)
{
scanf("%i %i %i", &X, &Y, &C);
capacity[X][Y] = C;
G[X].push_back(Y);
G[Y].push_back(X);
}
while(check())
{
for(it = G[N].begin(); it != G[N].end(); ++it)
if(used[*it] && capacity[*it][N] != currentFlow[*it][N])
{
int node = *it;
minFlow = INF;
father[N] = node;
for(node = N; node != 1; node = father[node])
minFlow = min(minFlow, capacity[father[node]][node] - currentFlow[father[node]][node]);
if(minFlow)
{
for(node = N; node != 1; node = father[node])
{
currentFlow[father[node]][node] += minFlow;
currentFlow[node][father[node]] -= minFlow;
}
maxFlow += minFlow;
}
}
}
printf("%i\n", maxFlow);
return 0;
}