Pagini recente » Cod sursa (job #2436757) | Cod sursa (job #1229837) | Cod sursa (job #523028) | Cod sursa (job #2084230) | Cod sursa (job #2961112)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
struct str {
int y, c;
str(){y = -1;}
str(int y, int c){
this->y = y, this->c = c;
}
};
queue<int> bfs;
vector<str> parent;
vector<vector<str>> lista;
int flow[1001][1001];
int bottleneck(int i){
if(parent[i].y == 0) return 111000;
return min(parent[i].c - flow[parent[i].y][i], bottleneck(parent[i].y));
}
void update(int b, int i){
if(parent[i].y == 0) return;
flow[parent[i].y][i] += b;
flow[i][parent[i].y] -= b;
update(b, parent[i].y);
}
int main(){
int n, m, x, y, z;
fin>>n>>m;
lista.resize(n+1);
for(int i=1; i<=m; i++){
fin>>x>>y>>z;
lista[x].push_back({y, z});
lista[y].push_back({x, 0});
}
parent.resize(n+1);
int b;
while(true){
bfs.push(1);
parent[1] = {0, 0};
while(!bfs.empty()){
x = bfs.front();
bfs.pop();
for(auto a: lista[x])
{
if(parent[a.y].y == -1 && flow[x][a.y] < a.c){
parent[a.y] = {x, a.c};
bfs.push(a.y);
}
}
}
if(parent[n].y != -1){
b = bottleneck(n);
update(b, n);
fill(parent.begin(), parent.end(), str());
}
else break;
}
int F = 0;
for(int i=1; i<=n; i++)
F += flow[1][i];
fout<<F;
}