Pagini recente » Cod sursa (job #2360699) | Cod sursa (job #1540310) | Cod sursa (job #359453) | Cod sursa (job #1877013) | Cod sursa (job #2389970)
#include <fstream>
#include <queue>
using namespace std;
ifstream cin("maxflow.in");
ofstream cout("maxflow.out");
const int N = 1000 + 7;
int n, m;
int g[N][N];
int level[N];
int nodes[N], y;
bool gasit = 0;
int res = 0;
void send(int nod, int MinFlow)
{
if(nod != 1)
{
for(int a = 1; a <= n; a++)
{
if(level[a] + 1 == level[nod] && g[a][nod] > 0)
{
nodes[++y] = a;
send(a, min(MinFlow, g[a][nod]));
y--;
}
}
return;
}
MinFlow = (1 << 30);
for(int i = y; i > 1; i--)
{
int a = nodes[i];
int b = nodes[i - 1];
MinFlow = min(MinFlow, g[a][b]);
}
for(int i = y; i > 1; i--)
{
int a = nodes[i];
int b = nodes[i - 1];
g[a][b] -= MinFlow;
g[b][a] += MinFlow;
}
if(MinFlow > 0)
{
gasit = 1;
}
res += MinFlow;
}
bool BFS()
{
gasit = 0;
for(int i = 1; i <= n; i++)
{
level[i] = -1;
}
level[1] = 0;
queue <int> q;
q.push(1);
while(!q.empty())
{
int nod = q.front();
q.pop();
for(int nou = 1; nou <= n; nou++)
{
if(g[nod][nou] > 0 && level[nou] == -1)
{
level[nou] = 1 + level[nod];
q.push(nou);
}
}
}
nodes[++y] = n;
send(n, (1 << 30));
y--;
return gasit;
}
int main()
{
cin >> n >> m;
while(m--)
{
int x, y, z;
cin >> x >> y >> z;
g[x][y] += z;
}
int cnt = 0;
while(BFS())
{
}
cout << res << "\n";
return 0;
}