Pagini recente » Cod sursa (job #985299) | Cod sursa (job #2872833) | Cod sursa (job #3212470) | Cod sursa (job #3124024) | Cod sursa (job #2591894)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
bool viz[MAXN];
queue<int> coada;
int flux[MAXN][MAXN], sursa[MAXN];
vector<int> graf[MAXN];
int main()
{
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n, m;
fin >> n >> m;
for(int i = 1; i <= m; ++i){
int x, y, c;
fin >> x >> y >> c;
flux[x][y] = c;
graf[x].push_back(y);
graf[y].push_back(x);
}
bool sink = 1;
int maxflux = 0;
while(sink){
sink = 0;
memset(viz, 0, n + 1);
memset(sursa, 0, n + 1);
coada.push(1);
viz[1] = 1;
while(!coada.empty()){
int vf = coada.front();
coada.pop();
for(auto x: graf[vf]){
if(!viz[x] && flux[vf][x] > 0){
viz[x] = 1;
sursa[x] = vf;
if(x == n) sink = 1;
else coada.push(x);
}
}
}
int mnflux = 1e9, crt = n;
while(crt > 1){
mnflux = min(mnflux, flux[sursa[crt]][crt]);
crt = sursa[crt];
}
crt = n;
while(crt > 1){
flux[sursa[crt]][crt] -= mnflux;
flux[crt][sursa[crt]] += mnflux;
crt = sursa[crt];
}
maxflux += mnflux;
}
fout << maxflux;
return 0;
}