Cod sursa(job #593549)

Utilizator perticas_catalinperticas catalin perticas_catalin Data 3 iunie 2011 14:06:17
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

#define NM 1005
#define PB push_back
#define inf 2000000000

int C[NM][NM], F[NM][NM], flux, N, M;

vector <int> G[NM];

int poti_pompa()
{
    int coada[NM], viz[NM], t[NM], st = 0, dr = -1;

    memset (viz, 0, sizeof(viz));

    coada[++dr] = 1;
    viz[1] = 1;
    t[1] = 0;

    while (st <= dr)
    {
        int nod = coada[st];
        ++st;

        if (nod == N) break;

        for (int i = 0; i < G[nod].size(); ++i)
        {
            int nnod = G[nod][i];
            if (viz[nnod]) continue;
            if (!(C[nod][nnod] - F[nod][nnod])) continue;
            coada[++dr] = nnod;
            viz[nnod] = 1;
            t[nnod] = nod;
        }
    }

    if (!viz[N]) return 0;

    int nod = N, flux_plus = inf;

    while (t[nod])
    {
        int tnod = t[nod];
        flux_plus = min (flux_plus, C[tnod][nod] - F[tnod][nod]);
        nod = tnod;
    }

    flux += flux_plus;

    nod = N;

    while (t[nod])
    {
        int tnod = t[nod];
        F[tnod][nod] += flux_plus;
        F[nod][tnod] -= flux_plus;
        nod = tnod;
    }

    return 1;
}

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

    scanf ("%d %d", &N, &M);

    for (int i = 1; i <= M; ++i)
    {
        int a, b, c;
        scanf ("%d %d %d", &a, &b, &c);

        G[a].PB(b);
        G[b].PB(a);

        C[a][b] += c;
    }

    while (poti_pompa());

    printf ("%d", flux);

    return 0;
}