Pagini recente » Cod sursa (job #2665844) | Cod sursa (job #1705749) | Cod sursa (job #1246504) | Cod sursa (job #74593) | Cod sursa (job #2939868)
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
using namespace std;
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");
const int INF = 2e9;
const int MAX_N = 1005;
int n, m, minflow, flux;
int parent[MAX_N], cap[MAX_N][MAX_N], flow[MAX_N][MAX_N];
vector <int> v[MAX_N];
bool viz[MAX_N];
static inline bool find_path(){
for(int i=1; i<=n; i++)
viz[i] = false;
queue <int> path;
viz[1] = true;
parent[1] = 0;
path.push(1);
int crt;
while(!path.empty()){
crt = path.front();
path.pop();
for(auto nxt : v[crt]){
if(!viz[nxt] && flow[crt][nxt] < cap[crt][nxt]){
viz[nxt] = true;
parent[nxt] = crt;
path.push(nxt);
if(nxt == n)
return true;
}
}
}
return false;
}
int main (){
ios_base::sync_with_stdio(false);
fin.tie(nullptr), fout.tie(nullptr);
fin>>n>>m;
for(int i=1, x, y, c; i<=m; i++){
fin>>x>>y>>c;
v[x].emplace_back(y);
v[y].emplace_back(x);
cap[x][y] = c;
}
while(find_path())
for(auto crt : v[n])
if(viz[crt] && flow[crt][n] < cap[crt][n]){
int nod;
parent[n] = crt;
minflow = INF;
nod = n;
while(parent[nod]){
minflow = min(minflow, cap[parent[nod]][nod] - flow[parent[nod]][nod]);
nod = parent[nod];
}
nod = n;
while(parent[nod]){
flow[parent[nod]][nod] += minflow;
flow[nod][parent[nod]] -= minflow;
nod = parent[nod];
}
flux += minflow;
}
fout<<flux;
return 0;
}