Cod sursa(job #1532801)

Utilizator ZenusTudor Costin Razvan Zenus Data 21 noiembrie 2015 16:49:19
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax = 1000 + 10;

vector < int > g[nmax];
queue < int > iq;
int n , m , a , b , z , flow , q0 , i , t , w0;
int f[nmax][nmax] , c[nmax][nmax];
int w[nmax] , p[nmax];

bool path()
{
    while (iq.size()) iq.pop();
    memset(p , 0 , sizeof(p));

    iq.push(1);
    p[1] = true;

    while (iq.size())
    {
        int q0 = iq.front();
        iq.pop();

        for (int i = 0 ; i < g[q0].size() ; ++i)
        {
            int nq = g[q0][i];
            if (p[nq]) continue;

            if (c[q0][nq] > f[q0][nq])
            {
                w[nq] = q0;
                iq.push(nq);
                p[nq] = true;
            }
        }
    }

    return p[n];
}

int main()
{
freopen("maxflow.in" , "r" , stdin);
freopen("maxflow.out" , "w" , stdout);

scanf("%d" , &n);
scanf("%d" , &m);

for (i = 1 ; i <= m ; ++i)
{
    scanf("%d" , &a);
    scanf("%d" , &b);
    scanf("%d" , &z);

    g[a].push_back(b);
    g[b].push_back(a);
    c[a][b] = z;
}

while (path())
{
    t = 1 << 30;

    for (q0 = n ; q0 > 1 ; q0 = w[q0])
    {
        w0 = w[q0];
        t = min(t , c[w0][q0] - f[w0][q0]);
    }

    flow += t;

    for (q0 = n ; q0 > 1 ; q0 = w[q0])
    {
        w0 = w[q0];
        f[w0][q0] += t;
        f[q0][w0] -= t;
    }
}

printf("%d\n" , flow);

return 0;
}