Pagini recente » Cod sursa (job #2841061) | Cod sursa (job #937186) | Cod sursa (job #329860) | Cod sursa (job #2217219) | Cod sursa (job #2588552)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1010;
struct obj{
int x, cost;
};
int N, M, flux, add_flux;
int cap[NMAX];
pair <int, int> prov[NMAX];
vector <obj> edges[NMAX];
queue <int> Q;
void read(){
scanf("%d%d", &N, &M);
int a, b, c;
for(int i = 1; i <= M; i++){
scanf("%d%d%d", &a, &b, &c);
edges[a].push_back({b, c});
}
add_flux = 1;
}
void bellman_ford(){
Q.push(1);
cap[1] = 2e9;
while(!Q.empty()){
int node = Q.front();
Q.pop();
for(int i = 0; i < edges[node].size(); i++){
obj neigh = edges[node][i];
if(cap[neigh.x] < min(cap[node], neigh.cost)){
cap[neigh.x] = min(cap[node], neigh.cost);
prov[neigh.x] = {node, i};
Q.push(neigh.x);
}
}
}
add_flux = cap[N];
pair <int, int> x = prov[N];
int node = N;
bool ok = false;
while(x.first != 0){
edges[x.first][x.second].cost -= add_flux;
for(int i = 0; i < edges[node].size(); i++)
if(edges[node][i].x == x.first){
edges[node][i].cost += add_flux;
ok = true;
}
if(!ok)
edges[node].push_back({x.first, add_flux});
x = prov[x.first];
}
}
int main(){
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
read();
while(add_flux){
add_flux = 0;
for(int i = 1; i <= N; i++){
cap[i] = 0;
prov[i] = {0, 0};
}
bellman_ford();
flux += add_flux;
}
printf("%d", flux);
return 0;
}