Cod sursa(job #2978335)

Utilizator sandry24Grosu Alexandru sandry24 Data 13 februarie 2023 17:58:57
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.64 kb
#include <bits/stdc++.h>
using namespace std;

//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second

int maxn = 1001;
vector<vi> adj(maxn), cap(maxn, vi(maxn));
vector<bool> visited(maxn);
vi parent(maxn);

int getmaxflow(int s, int d){
    for(int i = 0; i < maxn; i++)
        visited[i] = parent[i] = 0;
    queue<pi> q;
    q.push({s, 1e9});
    visited[s] = 1;
    while(!q.empty()){
        int u = q.front().f;
        int flow = q.front().s;
        q.pop();
        if(u == d)
            return flow;
        for(auto v : adj[u]){
            if(cap[u][v] == 0 || visited[v])
                continue;
            parent[v] = u;
            visited[v] = 1;
            q.push({v, min(flow, cap[u][v])});
        }
    }
    return 0;
}

void solve(){
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int x, y, c;
        cin >> x >> y >> c;
        adj[x].pb(y);
        adj[y].pb(x);
        cap[x][y] = c;
    }
    int ans = 0;
    while(int flow = getmaxflow(1, n)){
        ans += flow;
        for(int u = n; u != 1; u = parent[u]){
            int v = parent[u];
            cap[v][u] -= flow;
            cap[u][v] += flow;
        }
    }
    cout << ans << '\n';
}
 
int main(){
    freopen("maxflow.in", "r", stdin);
    freopen("maxflow.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}