Cod sursa(job #941622)

Utilizator xxxcnmvxxxnume cpmplet xxxcnmvxxx Data 19 aprilie 2013 10:50:05
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.71 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream in ("maxflow.in");
ofstream out ("maxflow.out");

const int MAXN = 1010;
const int INF = 1 << 30;

int N, M, S, D;
vector <int> Graf[MAXN];
int C[1010][1010], F[1010][1010], Viz[1010], T[1010];
queue <int> Q;

bool BFS ()
{
    vector <int> :: iterator it;
    int nod;

    for (nod = 1; nod <= N; nod ++)
        Viz[nod] = 0;

    Q.push (1);
    Viz[1] = 1;
    while (!Q.empty ()){
        nod = Q.front ();
        Q.pop ();

        if (nod == N)
            continue;

        for (it = Graf[nod].begin (); it != Graf[nod].end (); ++ it)
            if (!Viz[*it] && F[nod][*it] != C[nod][*it]){
                T[*it] = nod;
                Viz[*it] = 1;
                Q.push (*it);
            }
    }

    return Viz[N];
}

inline int flux ()
{
    vector <int> :: iterator it;
    int nod, mn, fm = 0, flow = 0;

    while (BFS ())
        for (it = Graf[N].begin (); it != Graf[N].end (); ++ it){
            T[N] = *it;
            fm = INF;

            for (nod = N; nod != 1; nod = T[nod])
                fm = min (fm, C[ T[nod] ][nod] - F[ T[nod] ][nod]);

            if (!fm)
                continue;

            for (nod = N; nod != 1; nod = T[nod]){
                F[ T[nod] ][nod] += fm;
                F[nod][ T[nod] ] -= fm;
            }

            flow += fm;
        }

    return flow;
}

int main()
{
    int i, a, b, c;

    in >> N >> M;
    while (M --){
        in >> a >> b >> c;
        Graf[a].push_back (b);
        Graf[b].push_back (a);
        C[a][b] = c;
    }

    out << flux ();

    return 0;
}