Pagini recente » Cod sursa (job #1698002) | Cod sursa (job #1112929) | Cod sursa (job #2964776) | Cod sursa (job #1006055) | Cod sursa (job #2747095)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define fr first
#define sc second
//#define int long long
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int nmax = 1005;
int n, m, capacity[nmax][nmax];
int bfs(int s, int t, vector<int>&parent){
for(int i=1;i<=n;i++){
parent[i] = -1;
}
parent[s] = 0;
queue<pair<int,int>>Q;
Q.push({s, 1e6});
while(Q.size()){
auto it = Q.front();
if(it.fr == t){
return it.sc;
}
Q.pop();
for(int i=1;i<=n;i++){
if(parent[i] == -1 && capacity[it.fr][i]){
parent[i] = it.fr;
Q.push({i, min(it.sc, capacity[it.fr][i])});
}
}
}
return 0;
}
int maxflow(int s, int t){
int flow = 0;
vector<int>parent(nmax);
int new_flow;
while(new_flow = bfs(s, t, parent)){
flow += new_flow;
int cur = t;
while(parent[cur] != 0){
capacity[parent[cur]][cur] -= new_flow;
capacity[cur][parent[cur]] += new_flow;
cur = parent[cur];
}
}
return flow;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
cin >> n >> m;
for(int i=1;i<=m;i++){
int x, y, z;
cin >> x >> y >> z;
capacity[x][y] = z;
}
cout << maxflow(1, n) << '\n';
}