Pagini recente » Cod sursa (job #1116248) | Cod sursa (job #1148050) | Cod sursa (job #2905291) | Cod sursa (job #1620878) | Cod sursa (job #2961450)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1010;
int N, M, ansFlow;
bool wasSeen[NMAX];
int dad[NMAX];
int flow[NMAX][NMAX], capacity[NMAX][NMAX];
vector <int> edges[NMAX];
void read(){
scanf("%d%d", &N, &M);
int x, y, c;
for(int i = 1; i <= M; i++){
scanf("%d%d%d", &x, &y, &c);
edges[x].push_back(y);
edges[y].push_back(x);
capacity[x][y] = c;
}
}
bool generateFlow(int node){
for(int i = 1; i <= N; i++){
wasSeen[i] = false;
dad[i] = 0;
}
queue <int> Q;
Q.push(node);
wasSeen[node] = true;
while(!Q.empty()){
node = Q.front();
Q.pop();
for(auto it : edges[node]){
if(!wasSeen[it] && capacity[node][it] - flow[node][it] > 0){
wasSeen[it] = true;
dad[it] = node;
Q.push(it);
}
}
}
if(!dad[N])
return false;
node = N;
for(auto it : edges[node]){
if(capacity[it][node] - flow[it][node] > 0){
int MinFlow = capacity[it][node] - flow[it][node];
for(int j = it; j != 1; j = dad[j])
MinFlow = min(MinFlow, capacity[dad[j]][j] - flow[dad[j]][j]);
flow[it][node] += MinFlow;
flow[node][it] -= MinFlow;
for(int j = it; j != 1; j = dad[j]){
flow[dad[j]][j] += MinFlow;
flow[j][dad[j]] -= MinFlow;
}
ansFlow += MinFlow;
}
}
return true;
}
int main() {
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
read();
bool repeat = true;
while(repeat)
repeat = generateFlow(1);
printf("%d", ansFlow);
return 0;
}