Pagini recente » Cod sursa (job #310134) | Cod sursa (job #1629210) | Cod sursa (job #2315263) | Cod sursa (job #1962698) | Cod sursa (job #1165754)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int MAX_N = 1002;
const int INF = 0x3f3f3f;
int N, M, sol;
int F[MAX_N], Capacity[MAX_N][MAX_N], Flow[MAX_N][MAX_N];
vector < int > v[MAX_N];
queue < int > Q;
bool BFS(int st, int end) {
memset(F, 0, sizeof(F));
Q.push(st);
F[st] = st;
while(!Q.empty()) {
int x = Q.front();
Q.pop();
if(x == end)
continue;
for(int i = 0; i < (int) v[x].size(); ++i) {
int y = v[x][i];
if(F[y] || Capacity[x][y] <= Flow[x][y])
continue;
F[y] = x;
Q.push(y);
}
}
return F[end] != 0;
}
int main() {
ifstream f("maxflow.in");
ofstream g("maxflow.out");
f >> N >> M;
for(int i = 1, x, y, c; i <= M; ++i) {
f >> x >> y >> c;
v[x].push_back(y);
v[y].push_back(x);
Capacity[x][y] += c;
}
while(BFS(1, N)) {
for(int i = 0; i < (int) v[N].size(); ++i) {
int x = v[N][i];
if(F[x] == 0 || Capacity[x][N] <= Flow[x][N])
continue;
F[N] = x;
int flow = INF;
for(int x = N; x != 1; x = F[x])
flow = min(flow, Capacity[F[x]][x] - Flow[F[x]][x]);
for(int x = N; x != 1; x = F[x])
Flow[F[x]][x] += flow, Flow[x][F[x]] = -Flow[F[x]][x];
sol += flow;
}
}
g << sol << "\n";
f.close();
g.close();
return 0;
}