Pagini recente » Cod sursa (job #884298) | Cod sursa (job #3209044) | Cod sursa (job #2451809) | Cod sursa (job #3030538) | Cod sursa (job #2591890)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
bool viz[MAXN];
queue<int> coada;
int cap[MAXN][MAXN], 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;
cap[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);
coada.push(1);
viz[1] = 1;
while(!coada.empty()){
int vf = coada.front();
coada.pop();
for(auto x: graf[vf]){
if(!viz[x] && cap[vf][x] != flux[vf][x]){
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, cap[sursa[crt]][crt] - 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;
}