Pagini recente » Cod sursa (job #816553) | Cod sursa (job #2397282) | Cod sursa (job #1323020) | Cod sursa (job #2062896) | Cod sursa (job #2265369)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow1.out");
int n,m,x,y,c,maxflow = 0;
int GR[1010][1010];
int tata[1010], viz[1010];
vector <int> L[1010];
int BFS()
{
for (int i = 1 ; i <= n; i++)
tata[i] = 0, viz[i] = 0;
queue <int> Q;
Q.push(1);
tata[1] = 1;
while (!Q.empty())
{
x = Q.front();
Q.pop();
if (x == n) continue;
for (auto j : L[x])
if (GR[x][j] > 0 && viz[j] == 0 && j!=x)
{
Q.push(j);
viz[j] = 1;
tata[j] = x;
}
}
return viz[n];
}
int main()
{
fin >> n >> m;
for (int k = 0; k < m; k++)
{
fin >> x >> y >> c;
GR[x][y] = c;
L[x].push_back(y);
L[y].push_back(x);
}
while (BFS())
{
for (auto j:L[n])
{
if ( GR[j][n] == 0 || !viz[j]) continue;
tata[n] = j;
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;
}