Pagini recente » Cod sursa (job #2732472) | Cod sursa (job #290649) | Cod sursa (job #2249665) | Cod sursa (job #1637270) | Cod sursa (job #2961582)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
vector<vector<int>>Lista;
vector<int>viz;
vector<int>t;
int flux[1001][1001];
int n,m,x,y,z,rez;
int BF(int s){
for(int i = 1;i<=n;i++){
t[i] = 0;
viz[i] = 0;
}
queue<int>q;
int vf = s;
viz[vf] = 1;
q.push(vf);
while(!q.empty()){
vf = q.front();
q.pop();
for(auto aux:Lista[vf]){
if(!viz[aux] && flux[vf][aux]>0) {
q.push(aux);
t[aux] = vf;
viz[aux] = 1;
if(aux == n){
return 1;
}
}
}
}
return 0;
}
int main() {
f>>n>>m;
Lista.resize(n+1);
viz.resize(n+1);
t.resize(n+1);
while(f){
f>>x>>y>>z;
Lista[x].push_back(y);
Lista[y].push_back(x);
flux[x][y] = z;
}
while(BF(1)){
for(auto aux: Lista[n]){
if(!viz[aux])
continue;
int minim = INT_MAX;
int nod = n;
while(nod!=1){
int tata = t[nod];
minim = min(minim,flux[tata][nod]);
nod = tata;
}
rez+=minim;
nod = n;
while(nod!=1){
int tata = t[nod];
flux[tata][nod] -= minim;
flux[nod][tata] += minim;
nod = tata;
}
}
}
g<<rez;
}