Pagini recente » Cod sursa (job #2161416) | Cod sursa (job #220535) | Cod sursa (job #2421994)
#include <iostream>
#include <fstream>
#include <queue>
#define MAXN 1001
#define INF 1<<30
#define DEST N
#define START 1
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
vector<int> G[MAXN];
int C[MAXN][MAXN], F[MAXN][MAXN];
int N, M, father[MAXN];
bool bfs() {
int node, next;
bool viz[MAXN] = {false};
queue<int> Q;
Q.push(START);
viz[START] = true;
while (!Q.empty()) {
node = Q.front();
Q.pop();
for (int i = 0; i < G[node].size(); ++i) {
next = G[node][i];
if (viz[next] == false && C[node][next] != F[node][next]) {
viz[next] = true;
Q.push(next);
father[next] = node;
if (next == DEST) {
return true;
}
}
}
}
return false;
}
int main()
{
int x, y, capacity, flux_min;
f >> N >> M;
for (int i = 1; i <= M; ++i) {
f >> x >> y >> capacity;
C[x][y] = capacity;
G[x].push_back(y);
G[y].push_back(x);
}
int flux = 0;
while (bfs() == true) {
flux_min = INF;
int node = DEST;
while (node != START) {
flux_min = min(flux_min, C[father[node]][node] - F[father[node]][node]);
node = father[node];
}
node = DEST;
while (node != START) {
F[father[node]][node] += flux_min;
F[node][father[node]] -= flux_min;
node = father[node];
}
flux += flux_min;
}
g << flux;
return 0;
}