Cod sursa(job #2204552)

Utilizator mlc_oficialBoris Barca mlc_oficial Data 16 mai 2018 14:07:19
Problema Flux maxim Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>

using namespace std;

const int kMaxN = 1e3, kMaxM = 5e3;

struct Edge {
    int x, y, c, f;
    int Other(int node)    const { return x ^ y ^ node; }
    int Capacity(int node) const { return (node == x) ? c - f : f; }
    void Push(int node)          { f += ((node == x) << 1) - 1; }
};

Edge e[kMaxM];
vector<int> graph[kMaxN];
bool vis[kMaxN];
int n;

bool Df(int node) {
    if (node == n - 1) return true;
    
    vis[node] = true;
    for (auto eidx : graph[node]) {
        auto&& e = ::e[eidx];
        if (not vis[e.Other(node)] 
                and e.Capacity(node) != 0 
                and Df(e.Other(node))) {
            e.Push(node);
            return true;
        }
    }    
    return false;
}

int main() {
    #ifdef INFOARENA
    freopen("maxflow.in", "r", stdin);
    freopen("maxflow.out", "w", stdout);
    #endif
    
    int m; cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int x, y, c; cin >> x >> y >> c; --x; --y;
        e[i] = {x, y, c, 0};
        graph[x].push_back(i);
        graph[y].push_back(i);
    }
    
    int fl = 0;
    while (Df(0)) {
        memset(vis, 0, n);
        ++fl;
    }
    
    cout << fl << endl;
}