Pagini recente » Cod sursa (job #1395268) | Cod sursa (job #1164336) | Cod sursa (job #928747) | Cod sursa (job #2063825) | Cod sursa (job #1875490)
# include <fstream>
# include <vector>
# include <bitset>
# include <queue>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int MAX = 1024;
const int INF = (1<<30);
typedef int matrice[MAX][MAX];
bitset<MAX> viz;
vector<int> L[MAX], t(MAX);
queue<int> q;
matrice c, f;
int n, m;
bool BFS() {
int nod, vec;
for (int i=2; i<=n; ++i)
viz[i] = 0;
while (!q.empty()) {
q.pop();
}
q.push(1);
viz[1] = 1;
vector<int>::iterator i;
while (!q.empty()) {
nod = q.front();
q.pop();
for (i = L[nod].begin(); i != L[nod].end(); ++i) {
vec = *i;
if (c[nod][vec] > f[nod][vec] && viz[vec] == 0) {
viz[vec] = 1;
q.push(vec);
t[vec] = nod;
if (vec == n)
return 1;
}
}
}
return 0;
}
int main() {
int x, y, z, flow, nod, fmin;
vector<int>::iterator i;
fin >> n >> m;
for (int i=1; i<=m; ++i) {
fin >> x >> y >> z;
c[x][y] += z;
c[y][x] = 0;
L[x].push_back(y);
L[y].push_back(x);
}
flow = 0;
while (BFS()) {
//for (i = L[n].begin(); i != L[n].end(); ++i) {
//nod = *i;
//if (f[nod][n] != c[nod][n] && viz[nod]) {
//t[n] = nod;
fmin = INF;
for (nod = n; nod != 1; nod = t[nod])
fmin = min(fmin, c[ t[nod] ][nod] - f[ t[nod] ][nod]);
if (fmin != 0) {
for (nod = n; nod != 1; nod = t[nod]) {
f[ t[nod] ][nod] += fmin;
f[nod][ t[nod] ] -= fmin;
}
flow += fmin;
}
//}
//}
}
fout << flow;
return 0;
}