Pagini recente » Cod sursa (job #918423) | Cod sursa (job #1416621) | Cod sursa (job #826521) | Cod sursa (job #151224) | Cod sursa (job #1877455)
#include <bits/stdc++.h>
#define NMAX 1005
#define INF 1e9
using namespace std;
int F[NMAX][NMAX];
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");
vector < int > G[NMAX];
bitset < NMAX > B;
deque < int > Q;
int ant[NMAX];
int BFS(int n){
int nod;
ant[1] = 1;
memset (B,0,B.size());
B[1] = 1;
Q.push_back(1);
while(!Q.empty()){
nod = Q.front();
Q.pop_front();
for(auto const &i = G[nod]){
if(!B[i] && F[nod][i] > 0){
if(i == n)
return 1;
Q.push_back(i);
B[i] = 1;
ant[i] = nod;
}
}
}
return 0;
}
int main()
{
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,mflow;
while(BFS(n)){
for(auto const &i = G[nod]){
mflow = INF;
for(int i = d; i != ant[i]; i = ant[i])
mflow = min(mflow , F[ans[i]][i]);
for(int i = d; i != ant[i]; i = ant[i]){
F[ant[i]][i] -= mflow;
F[i][ant[i]] += mflow;
}
sol += mflow;
}
}
fout << sol;
return 0;
}