Pagini recente » Cod sursa (job #2213368) | Cod sursa (job #1763486) | Cod sursa (job #2731939) | Cod sursa (job #2999571) | Cod sursa (job #2984123)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
//////////////////////////////////////
ifstream cin("maxflow.in");
ofstream cout("maxflow.out");
const int MAX = 1e3 + 1;
const int inf = 1e9;
int capacitate[MAX][MAX] , flow[MAX][MAX] , n , m , x , y , c , t[MAX] , fl , gfl;
bool in[MAX];
vector <int> g[MAX];
int bfs(){
for(int i = 1 ; i <= n ; i++){
in[i] = t[i] = 0;
}
queue <pair<int,int>> q;
t[1] = 0;
q.push({1,inf});
while(!q.empty()){
x = q.front().first;
fl = q.front().second;
q.pop();
in[x] = 1;
if( x == n ) return fl;
for(auto it : g[x]){
int auxfl = min(fl,capacitate[x][it]-flow[x][it]);
if(!in[it] && auxfl>0){
t[it] = x;
q.push({it,auxfl});
in[it] = 1;
}
}
}
return 0;
}
int main()
{
cin >> n >> m;
while(m--){
cin >> x >> y >> c;
capacitate[x][y] = c;
g[x].push_back(y);
g[y].push_back(x);
}
while(1){
bool ok = 0;
for(auto it : g[n]){
if(capacitate[it][n] - flow[it][n] > 0) ok = 1;
}
if(!ok) break;
int new_flow = bfs();
if(!new_flow) break;
gfl += new_flow;
int aux = n;
while( t[aux] ){
flow[t[aux]][aux] += new_flow;
flow[aux][t[aux]] -= new_flow;
aux = t[aux];
}
}
cout << gfl;
return 0;
}