Pagini recente » Cod sursa (job #473097) | Cod sursa (job #2999215) | Cod sursa (job #2636402) | Cod sursa (job #2783474) | Cod sursa (job #1879060)
#include <bits/stdc++.h>
#define NMAX 1015
#define INF (1LL<<31)-1
using namespace std;
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");
vector < int > G[NMAX];
int F[NMAX][NMAX],ant[NMAX];
bool B[NMAX];
int BFS(const int &n){
queue < int > Q;
int nod;
memset(B,0,sizeof(B));
B[1] = 1; ant[1] = 1;
Q.push(1);
while(!Q.empty()){
nod = Q.front();
Q.pop();
for(auto const &it : G[nod]){
if(!B[it] && F[nod][it] > 0){
B[it] = 1;
if(it == n)
return 1;
Q.push(it);
ant[it] = nod;
}
}
}
return 0;
return B[n];
}
int main()
{
ios :: sync_with_stdio(false);
fin.tie(NULL);
int n,m,x,y,c;
fin >> n >> m;
for(int i = 1; i <= m; i++){
fin >> x >> y >> c;
G[x].push_back(y);
G[y].push_back(x);
F[x][y] += c;
}
int sol = 0, mflux;
while(BFS(n)){
for(auto const &it : G[n]){
if(B[it] == 1 && F[it][n] > 0){
ant[n] = it;
mflux = INF;
for(int j = n; j != ant[j]; j = ant[j])
mflux = min(mflux,F[ant[j]][j]);
for(int j = n; j != ant[j]; j = ant[j]){
F[ant[j]][j] -= mflux;
F[j][ant[j]] += mflux;
}
sol += mflux;
}
}
}
fout << sol;
return 0;
}