Pagini recente » Cod sursa (job #644648) | Cod sursa (job #3337013) | Cod sursa (job #3316337) | Cod sursa (job #3337003) | Cod sursa (job #3337007)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstring>
#include <climits>
using namespace std;
const int N=1001;
vector <int> L[N];
int viz[N];
int flux[N][N];
int cap[N][N];
int tata[N];
int BFS(int s, int t){
queue <pair<int,int>> q;
viz[s]=1;
q.push({s, INT_MAX});
while(!q.empty()){
int nod=q.front().first;
int f=q.front().second;
q.pop();
for(auto vecin: L[nod]){
int rez=cap[nod][vecin] - flux[nod][vecin];
if(!viz[vecin] && rez>0){
viz[vecin]=1;
tata[vecin]=nod;
if(rez<f){
f=rez;
}
if(vecin==t){
int crt=nod;
while(crt!=s){
int ta=tata[crt];
flux[ta][crt]+=f;
flux[crt][ta]-=f;
crt=ta;
}
return f;
}
q.push({vecin,f});
}
}
}
return 0;
}
int main(){
ifstream cin("maxflow.in");
ofstream cout("maxflow.out");
int n, m;
cin>>n>>m;
for(int i = 0; i < m; i++){
int x, y, c;
cin>>x>>y>>c;
L[x].push_back(y);
L[y].push_back(x);
cap[x][y]=c;
flux[x][y]=0;
}
int flux_max=0;
int s=1, t=n;
while(true){
int flux=BFS(s,t);
if(!flux){
break;
}
flux_max+=flux;
for(int i=1; i<=n; i++){
viz[i]=0;
}
}
cout<<flux_max;
return 0;
}