Pagini recente » Cod sursa (job #706563) | Cod sursa (job #1476652) | Cod sursa (job #2279090) | Cod sursa (job #2989136) | Cod sursa (job #2387990)
#include <iostream>
#include <fstream>
#include <deque>
#include <cstring>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int dmax = 1e3 + 5;
const int INF = 1e9;
int n, m;
int x, y, z;
int ResGraph[dmax][dmax];
int parent[dmax];
int newV, pVerif, newFlow;
bool newPath, viz[dmax];
int maxFlow;
deque<int> d;
bool BFS() {
int i, j;
memset(viz, false, sizeof(viz));
memset(parent, false, sizeof(viz));
viz[1] = true;
newPath = false;
d.push_back(1);
while (!d.empty()) {
pVerif = d.front();
d.pop_front();
for (i = 1; i <= n; i++) {
if (ResGraph[pVerif][i] && !viz[i]) {
viz[i] = true;
if (i == n) {
newPath = true;
continue;
}
d.push_back(i);
parent[i] = pVerif;
}
}
}
return newPath;
}
void FindMinFlow() {
int i, j;
newFlow = INF;
pVerif = n;
while (pVerif != 1) {
newFlow = min(newFlow, ResGraph[parent[pVerif]][pVerif]);
pVerif = parent[pVerif];
}
}
void AugmentPath() {
int i, j;
pVerif = n;
while (pVerif != 1) {
ResGraph[parent[pVerif]][pVerif] -= newFlow;
ResGraph[pVerif][parent[pVerif]] += newFlow;
pVerif = parent[pVerif];
}
maxFlow += newFlow;
}
void FordFulkerson() {
int i, j;
while (BFS()) {
for (i = 1; i <= n; i++) {
if (ResGraph[i][n] && viz[i]) {
parent[n] = i;
FindMinFlow();
AugmentPath();
}
}
}
}
int main() {
int i, j;
fin >> n >> m;
for (i = 1; i <= m; i++) {
fin >> x >> y >> z;
ResGraph[x][y] += z;
}
FordFulkerson();
fout << maxFlow;
return 0;
}