Pagini recente » Cod sursa (job #1974693) | Cod sursa (job #2739346) | Cod sursa (job #2320609) | Cod sursa (job #1975295) | Cod sursa (job #3156213)
#include <bits/stdc++.h>
#pragma GCC optimize ("O3,unroll-loops")
#pragma GCC target ("popcnt")
using namespace std;
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");
const int MAX_N = 1000;
const int MAX_M = 5000;
int n, m;
vector<int> edge[MAX_N + 5];
int cap[MAX_N + 5][MAX_N + 5], flow[MAX_N + 5][MAX_N + 5];
int tata[MAX_N + 5];
static inline bool find_paths(){
queue<int> path;
for(int i=1; i<=n; i++)
tata[i] = -1;
tata[1] = 0;
path.push(1);
int crt;
while(!path.empty()){
crt = path.front();
path.pop();
for(auto nxt : edge[crt])
if(tata[nxt] == -1 && flow[crt][nxt] < cap[crt][nxt]){
tata[nxt] = crt;
path.push(nxt);
}
}
if(tata[n] != -1)
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, u, v, c; i<=m; i++){
fin>>u>>v>>c;
edge[u].push_back(v);
edge[v].push_back(u);
cap[u][v] = c;
}
int flux = 0, minflow, nod;
while(find_paths()){
for(auto crt : edge[n]){
if(tata[crt] != -1 && flow[crt][n] < cap[crt][n]){
minflow = INT_MAX;
tata[n] = crt;
nod = n;
while(nod != 1){
minflow = min(minflow, cap[tata[nod]][nod] - flow[tata[nod]][nod]);
nod = tata[nod];
}
nod = n;
while(nod != 1){
flow[tata[nod]][nod] += minflow;
flow[nod][tata[nod]] -= minflow;
nod = tata[nod];
}
flux += minflow;
}
}
}
fout<<flux;
return 0;
}
/**
Input:
**/