Cod sursa(job #1869788)

Utilizator andru47Stefanescu Andru andru47 Data 6 februarie 2017 10:18:02
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
#include <bits/stdc++.h>
using namespace std;
int n,m,capacitate[1005][1005],flux[1005][1005],tata[1005];
vector <int> g[1005];
queue<int> q;
inline bool still() {
    q.push(1);
    bool ok = false;
    memset(tata,0,sizeof(tata));
    tata[1] = -1;
    while(!q.empty()) {
        int node = q.front();
        for (auto &it : g[node])
            if (!tata[it] && capacitate[node][it] > flux[node][it]) {
                if (it == n) {
                    ok = true;
                    continue;
                }
                q.push(it);
                tata[it] = node;
            }
        q.pop();
    }
    return ok;
}
inline int maxflow() {
    int fluxx = 0;
    for (bool ok = still(); ok; ok = still())
        for (auto &it : g[n])
            if (tata[it] && capacitate[it][n] > flux[it][n]) {
                tata[n] = it;
                int minn = INT_MAX;
                int node = n;
                for ( ; node>1; node = tata[node])
                    minn = min(minn,capacitate[tata[node]][node] - flux[tata[node]][node]);
                fluxx += minn;
                node = n;
                for ( ; node>1; node = tata[node])
                    flux[tata[node]][node] += minn , flux[node][tata[node]] -= minn;

            }
    return fluxx;
}
int main() {
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
    scanf("%d %d\n", &n, &m);
    for (int i = 1; i<=m; ++i) {
        int x,y,c;
        scanf("%d %d %d\n", &x, &y, &c);
        capacitate[x][y] = c;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    printf("%d\n", maxflow());
    return 0;
}