Pagini recente » Diferente pentru utilizator/eclipse intre reviziile 6 si 5 | Cod sursa (job #2728360) | Cod sursa (job #2545604) | Cod sursa (job #2383155) | Cod sursa (job #2220063)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <deque>
#include <queue>
#define flow first
#define capacity second
#define dMAX (1 << 10)
#define INF (1 << 10)
using namespace std;
int n, m;
int x, y, c;
int u, pVerif, newV;
int fmin, maxFlow;
int capacity[dMAX + 2][dMAX + 2], flow[dMAX + 2][dMAX + 2];
vector<int> graf[dMAX + 2];
int father[dMAX + 2];
int myDeque[dMAX + 2], h;
int viz[dMAX + 2];
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
bool BFS() {
int i, j;
memset(viz, false, sizeof(viz));
viz[1] = true;
myDeque[1] = h = 1;
for (i = 1; i <= h; i++) {
pVerif = myDeque[i];
if (pVerif == n) continue;
for (u = 0; u < graf[pVerif].size(); u++) {
newV = graf[pVerif][u];
if (capacity[pVerif][newV] == flow[pVerif][newV] || viz[newV]) continue;
viz[newV] = true;
myDeque[++h] = newV;
father[newV] = pVerif;
}
}
return viz[n];
}
int main()
{
int i, j;
fin >> n >> m;
for (i = 1; i <= m; i++) {
fin >> x >> y >> c;
capacity[x][y] = c;
graf[x].push_back(y);
graf[y].push_back(x);
}
maxFlow = 0;
while (BFS()) {
for (i = 0; i < graf[n].size(); i++) {
fmin = INF;
pVerif = graf[n][i];
if (flow[pVerif][n] == capacity[pVerif][n] || !viz[pVerif]) continue;
father[n] = pVerif;
for (j = n; j != 1; j = father[j]) fmin = min(fmin, capacity[ father[j] ][j] - flow[ father[j] ][j]);
for (j = n; j != 1; j = father[j]) {
flow[ father[j] ][j] += fmin;
flow[j][ father[j] ] -= fmin;
}
maxFlow += fmin;
}
}
fout << maxFlow;
return 0;
}