Pagini recente » Cod sursa (job #1256807) | Cod sursa (job #1698648) | Cod sursa (job #1252689) | Cod sursa (job #1430) | Cod sursa (job #2265038)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n,m,x,y,c,maxflow = 0;
int GR[1010][1010];
int tata[1010];
bool BFS()
{
for (int i = 1 ; i <= n; i++)
tata[i] = 0;
queue <int> Q;
Q.push(1);
tata[1] = 1;
while (!Q.empty())
{
x = Q.front();
Q.pop();
for (int j = 1; j <= n; j++)
if (GR[x][j] > 0 && tata[j] == 0 && j!=x)
{
Q.push(j);
tata[j] = x;
if (j == n)
return true;
}
}
return false;
}
int main()
{
fin >> n >> m;
for (int k = 0; k < m; k++)
{
fin >> x >> y >> c;
GR[x][y] = c;
}
while (BFS())
{
x = n;
y = INT_MAX;
while (x != 1)
{
y = min(GR[tata[x]][x],y);
x = tata[x];
}
x = n;
while (x != 1)
{
GR[tata[x]][x] -= y;
GR[x][tata[x]] += y;
x = tata[x];
}
maxflow += y;
}
fout << maxflow;
return 0;
}