Pagini recente » Cod sursa (job #1719002) | Cod sursa (job #2284568) | Cod sursa (job #170764) | Cod sursa (job #2884787) | Cod sursa (job #2387984)
#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));
newPath = false;
d.push_back(1);
while (!d.empty()) {
pVerif = d.front();
d.pop_front();
viz[pVerif] = true;
for (i = 1; i <= n; i++) {
if (ResGraph[pVerif][i] && !viz[i]) {
if (i == n) newPath = true;
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]) {
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;
}