Pagini recente » Cod sursa (job #1350734) | Cod sursa (job #2357942) | Cod sursa (job #2580392) | Cod sursa (job #3180071) | Cod sursa (job #2919672)
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");
const int INF = 2e9;
const int MAX_N = 1005;
int n, m, x, y, c, minflow, flux;
int cap[MAX_N][MAX_N], flow[MAX_N][MAX_N];
vector <int> edge[MAX_N];
static inline void update_flow(int x, int y){
flow[x][y] += minflow;
flow[y][x] -= minflow;
}
int crt, parent[MAX_N];
bool viz[MAX_N];
static inline bool find_path(){
for(int i=1; i<=n; i++)
viz[i] = false;
viz[1] = true;
queue <int> path;
path.push(1);
while(!path.empty()){
crt = path.front();
path.pop();
for(auto nxt : edge[crt])
if(!viz[nxt] && flow[crt][nxt] < cap[crt][nxt]){
parent[nxt] = crt;
viz[nxt] = true;
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; i<=m; i++){
fin>>x>>y>>c;
edge[x].push_back(y);
edge[y].push_back(x);
cap[x][y] = c;
}
while(find_path())
for(auto leaf : edge[n])
if(viz[leaf] && flow[leaf][n] < cap[leaf][n]){
minflow = INF;
crt = n;
while(parent[crt]){
minflow = min(minflow, cap[ parent[crt] ][ crt ] - flow[ parent[crt] ][ crt ]);
crt = parent[crt];
}
flux += minflow;
crt = n;
while(parent[crt]){
update_flow(parent[crt], crt);
crt = parent[crt];
}
}
fout<<flux;
return 0;
}