Cod sursa(job #1532803)

Utilizator ZenusTudor Costin Razvan Zenus Data 21 noiembrie 2015 16:58:10
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 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 , d0;
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();
        if (q0 == n) continue;

        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())
for (i = 0 ; i < g[n].size() ; ++i)
{
    d0 = g[n][i];
    if (c[d0][n] == f[w0][q0]) continue;
    if (p[d0] == false) continue;

    t = 1 << 30;

    w[n] = d0;
    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;
}