Pagini recente » Cod sursa (job #476356) | Cod sursa (job #230202) | Cod sursa (job #1973609) | Cod sursa (job #29304) | Cod sursa (job #3254690)
#include <bits/stdc++.h>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int a[1001][1001], flux[1001][1001], n, m;
void cit()
{
f >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, k;
f >> x >> y >> k;
a[x][y] = k;
flux[x][y] = k;
}
}
vector <int> t(1001, 0);
void bfs()
{
queue <int> q;
q.push(1);
t[1] = -1;
while(!q.empty())
{
int nod = q.front();
q.pop();
for(int i = 1;i <= m; i++)
{
if(flux[nod][i] != 0 && !t[i])
{
q.push(i);
t[i] = nod;
}
}
}
}
bool areDrum()
{
fill(t.begin(), t.end(), 0);
bfs();
if(t[n] != 0)
return true;
return false;
}
void rez()
{
int sum = 0;
while(areDrum())
{
int capmin = INT_MAX;
for(int i = n; i != 1; i = t[i])
{
capmin = min(capmin, flux[t[i]][i]);
}
for(int i = n; i != 1; i = t[i])
{
flux[t[i]][i] -= capmin;
flux[i][t[i]] += capmin;
}
sum += capmin;
}
g << sum;
}
int main()
{
cit();
rez();
return 0;
}