Cod sursa(job #1409973)

Utilizator 4ONI2015oni2015 4ONI2015 Data 30 martie 2015 19:55:20
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>
#define N 1005
#define pb emplace_back
#define oo 1<<30

using namespace std;
vector<int>v[N];
queue<int>q;
int tata[N], cap[N][N], fl[N][N], x, z, y, n, m, minflow, sol;
bool bf(int nod)
{
    memset(tata, 0, sizeof(tata));
    tata[1] = 1;
    q.push(1);
    while(q.size())
    {
        nod = q.back();
        q.pop();
        if(nod == n)
            continue;
        for(auto it : v[nod])
            if(!tata[it] && cap[nod][it] > fl[nod][it])
            {
                tata[it] = nod;
                q.push(it);
            }
    }
    return tata[n];
}
int main()
{
    freopen("maxflow.in", "r", stdin);
    freopen("maxflow.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(; m; m--)
    {
        scanf("%d%d%d", &x, &y, &z);
        v[x].pb(y);
        v[y].pb(x);
        cap[x][y] = z;
    }
    while(bf(1))
    {
        for(auto it : v[n])
        {
            if(tata[it])
            {
                tata[n] = it;
                minflow = oo;
                for(x = n; x != tata[x]; x = tata[x])
                    minflow = min(minflow, cap[tata[x]][x] - fl[tata[x]][x]);
                for(x = n; x != tata[x]; x = tata[x])
                {
                    fl[tata[x]][x] += minflow;
                    fl[x][tata[x]] -= minflow;
                }
                sol += minflow;
            }
        }
    }
    printf("%d", sol);
    return 0;
}