Pagini recente » Cod sursa (job #817619) | Cod sursa (job #2536261) | Cod sursa (job #409220) | Cod sursa (job #310866) | Cod sursa (job #2950575)
#include <bits/stdc++.h>
#define dim 1010
#define inf INT_MAX
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
long long maxFlow = 0;
int tata[dim], a[dim][dim], n, m, x, y, z, nod, flow;
bool viz[dim];
queue< int > coada; //nodul si capacitatea minima pana la el
bool bfs() {
while(!coada.empty()) {
coada.pop();
}
memset(tata, 0, sizeof(tata));
memset(viz, false, sizeof(viz));
coada.push(1);
viz[1] = true;
while(!coada.empty()) {
int front = coada.front();
coada.pop();
if(front == n) {
return true;
}
for(int i = 1; i <= n; i++) {
if( !viz[i] && a[front][i] > 0) {
coada.push(i);
viz[i] = true;
tata[i] = front;
}
}
}
return false;
}
int main() {
fin >> n >> m;
for(int i = 0; i < m; i++) {
fin >> x >> y >> z;
a[x][y] = z;
}
while(bfs()) {
for(int i = 1; i < n; i++) {
if( viz[i] && a[i][n] > 0 ) {
flow = inf;
tata[n] = i;
for(nod = n; tata[nod] > 0; nod = tata[nod]) {
flow = min(flow, a[ tata[nod] ][nod]);
}
if(flow == 0) continue;
for(nod = n; tata[nod] > 0; nod = tata[nod]) {
a[ tata[nod] ][nod] -= flow;
a[nod][ tata[nod] ] += flow;
}
maxFlow += flow;
}
}
}
fout << maxFlow << '\n';
return 0;
}