Pagini recente » Cod sursa (job #1805286) | Cod sursa (job #1131835) | Cod sursa (job #1396334) | Cod sursa (job #2509341) | Cod sursa (job #1501326)
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#define NMAX 1005
#define inf 0x3f3f3f
using namespace std;
int N, M;
int C[NMAX][NMAX], flux[NMAX][NMAX];
int tata[NMAX];
queue <int> q;
vector <int> G[NMAX];
int bfs(){
int nod;
for(;!q.empty(); q.pop());
memset(tata, 0, sizeof(tata));
q.push(1);
while(!q.empty()){
nod = q.front();
q.pop();
for(vector <int> :: iterator it = G[nod].begin(); it != G[nod].end(); ++it)
if(!tata[*it] && (C[nod][*it] > 0 || flux[nod][*it] > 0)){
q.push(*it);
tata[*it] = nod;
if(*it == N) return 1;
}
}
return 0;
}
int main(){
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int r, i, x, y, c, fx;
scanf("%d %d", &N, &M);
for(int i = 1; i <= M; ++i){
scanf("%d %d %d", &x, &y, &c);
C[x][y] += c;
G[x].push_back(y);
G[y].push_back(x);
}
for(fx = 0; bfs(); fx += r){
r = inf;
i = N;
while(i != 1){
if(C[tata[i]][i] > 0)
r = min(r, C[tata[i]][i]);
else r = min(r, flux[tata[i]][i]);
i = tata[i];
}
i = N;
while(i != 1){
if(C[tata[i]][i] > 0){
C[tata[i]][i] -= r;
flux[i][tata[i]] += r;
}else
flux[tata[i]][i] -= r;
i = tata[i];
}
}
printf("%d\n", fx);
return 0;
}