Pagini recente » Cod sursa (job #1125605) | Cod sursa (job #1848408) | Cod sursa (job #2510000) | Cod sursa (job #1066146) | Cod sursa (job #2715377)
#include <fstream>
#include <queue>
using namespace std;
ifstream cin("maxflow.in");
ofstream cout("maxflow.out");
const int NMAX = 1000;
int N, M;
vector <int> g[NMAX + 2];
int capacity[NMAX + 2][NMAX + 2], flow[NMAX + 2][NMAX + 2];
bool seen[NMAX + 2];
int parent[NMAX + 2];
bool BFS() {
for(int i = 1; i <= N; i++) {
seen[i] = false;
parent[i] = 0;
}
queue <int> q;
q.push(1);
seen[1] = true;
while(!q.empty()) {
int node = q.front();
q.pop();
for(int it : g[node]) {
if(flow[node][it] < capacity[node][it] && seen[it] == false) {
seen[it] = true;
parent[it] = node;
q.push(it);
}
}
}
return (seen[N] == true);
}
int main() {
cin >> N >> M;
for(int i = 1; i <= M; i++) {
int x, y, c;
cin >> x >> y >> c;
g[x].push_back(y);
g[y].push_back(x);
capacity[x][y] = c;
}
while(BFS()) {
for(int pv : g[N]) {
int pumpFlow = capacity[pv][N] - flow[pv][N];
for(int node = pv; node != 1; node = parent[node]) {
pumpFlow = min(pumpFlow, capacity[parent[node]][node] - flow[parent[node]][node]);
}
flow[pv][N] += pumpFlow;
flow[N][pv] -= pumpFlow;
for(int node = pv; node != 1; node = parent[node]) {
flow[parent[node]][node] += pumpFlow;
flow[node][parent[node]] -= pumpFlow;
}
}
}
int ans = 0;
for(int it : g[1]) {
ans += flow[1][it];
}
cout << ans << '\n';
return 0;
}