Pagini recente » Cod sursa (job #1161682) | Cod sursa (job #2745807) | Cod sursa (job #1078686) | Cod sursa (job #761786) | Cod sursa (job #2957873)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int n,m;
vector<vector<int>> muchii;
int cp[1005][1005];
int gaseste_drum(){
vector<int> parinte(n+1,-1);
queue <int>q;
int node = 1;
parinte[1] = 0;
q.push(1);
while(!q.empty()){
node = q.front();
q.pop();
if(node != n) {
for (auto i: muchii[node]) {
if (parinte[i] == -1 and cp[node][i] > 0) {
q.push(i);
parinte[i] = node;
}
}
}
else {
int flux = 9999999;
while(node != 1){
flux = min(flux,cp[parinte[node]][node]);
node = parinte[node];
}
node = n;
while(node != 1){
cp[node][parinte[node]] +=flux;
cp[parinte[node]][node] -=flux;
node = parinte[node];
}
return flux;
}
}
return 0;
}
int main(){
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
fin >> n >> m;
muchii.resize(n+1);
while(m--){
int x,y,cap;
fin >> x >> y >> cap;
muchii[x].push_back(y);
muchii[y].push_back(x);
cp[x][y] = cap;
cp[y][x] = 0;
}
int ans = 0;
int x = gaseste_drum();
while(x != 0){
ans += x;
x = gaseste_drum();
}
fout << ans;
return 0;
};