Pagini recente » Cod sursa (job #1645243) | Cod sursa (job #3040555) | Cod sursa (job #2127112) | Cod sursa (job #1360536) | Cod sursa (job #1977087)
#include <bits/stdc++.h>
using namespace std;
#define NMAX 1001
#define INF 1e9
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int cap[NMAX][NMAX], flux[NMAX][NMAX];
vector<int> graf[NMAX];
int source, sink;
inline int addEdge(const int x, const int y, const int c) {
graf[x].push_back(y);
graf[y].push_back(x);
cap[x][y] = cap[y][x] = c;
}
int tata[NMAX];
int BFS() {
vector<int> dist(sink + 1, INF);
queue<int> Q;
dist[source] = 0;
Q.push(source);
while (!Q.empty()) {
int nod = Q.front();
Q.pop();
for (auto& adj: graf[nod]) {
if (dist[adj] > dist[nod] + 1 && flux[nod][adj] < cap[nod][adj]) {
dist[adj] = dist[nod] = 1;
tata[adj] = nod;
Q.push(adj);
}
}
}
return (dist[sink] != INF);
}
int maxFlow() {
int ans = 0;
while (BFS()) {
int flow = INF;
for (int i = sink; i != source; i = tata[i])
flow = min(flow, cap[tata[i]][i] - flux[tata[i]][i]);
ans += flow;
for (int i = sink; i != source; i = tata[i]) {
flux[tata[i]][i] += flow;
flux[i][tata[i]] -= flow;
}
}
return ans;
}
int main() {
int N, M;
fin >> N >> M;
source = 1;
sink = N;
int x, y, c;
while (M--) {
fin >> x >> y >> c;
addEdge(x, y, c);
}
fout << maxFlow();
return 0;
}