Pagini recente » Cod sursa (job #2863091) | Cod sursa (job #2133584) | Cod sursa (job #542988) | Cod sursa (job #156028) | Cod sursa (job #2957879)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int n,m;
vector<vector<int>> muchii;
int cp[1005][1005];
vector<int> parinte;
bool gaseste_drum(){
parinte = vector <int>(n+1,-1);
queue <int>q;
int node;
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;
}
}
}
}
return parinte[n] != -1;
}
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;
}
int ans = 0;
while(gaseste_drum()){
for (auto x: muchii[n]){
if(parinte[x] != -1){
int flux = 9999999;
int node = n;
parinte[n] = x;
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];
}
ans += flux;
}
}
}
fout << ans;
return 0;
};