Cod sursa(job #602912)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 13 iulie 2011 18:09:13
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.56 kb
/*#include <iostream>
#include <fstream>
#include <vector>
#include <deque>

#define Infinit 2000000000

using namespace std;

vector <int> G[1005];
int N, Flow[1005][1005], Father[1005], MaxFlow, Cap[1005][1005];
bool Viz[1005];
deque <int> Coada;


void Read ()
{
    ifstream fin ("maxflow.in");
    int M, X, Y, Z;
    fin >> N >> M;
    for (; M>0; --M)
    {
        fin >> X >> Y >> Z;
        G[X].push_back (Y);
        G[Y].push_back (X);
        Cap[X][Y]+=Z;
    }
    fin.close ();
}

void Type ()
{
    ofstream fout ("maxflow.out");
    fout << MaxFlow << "\n";
    fout.close ();
}

int BFS (int Start, int End)
{
    deque <int> :: iterator X;
    unsigned i;
    int V;
    for (i=1; i<=N; ++i)
    {
        Viz[i]=false;
    }
    Viz[Start]=true;
    Coada.push_back (Start);
    while (Coada.size ()>0)
    {
        X=Coada.begin ();
        Coada.pop_front ();
        if (*X==End)
        {
            continue;
        }
        for (i=0; i<G[*X].size (); ++i)
        {
            V=G[*X][i];
            if ((Viz[V]==false)&&(Cap[*X][V]-Flow[*X][V]>0))
            {
                Coada.push_back (V);
                Father[V]=*X;
                Viz[V]=true;
            }
        }
    }
    if (Viz[End]==true)
    {
        return 1;
    }
    return 0;
}

inline int Min (int a, int b)
{
    if (a<b)
    {
        return a;
    }
    return b;
}

int main()
{
    unsigned i;
    int X, F, CurrentF;
    Read ();
    for (MaxFlow=0; BFS (1, N)!=0; MaxFlow+=F)
    {
        F=0;
        for (i=0; i<G[N].size (); ++i)
        {
            X=G[N][i];
            if ((Viz[X]==true)&&(Cap[X][N]-Flow[X][N]>0))
            {
                Father[N]=X;
                CurrentF=Infinit;
                for (X=N; X>1; X=Father[X])
                {
                    CurrentF=Min (Cap[Father[X]][X]-Flow[Father[X]][X], CurrentF);
                }
                if (CurrentF>0)
                {
                    for (X=N; X>1; X=Father[X])
                    {
                        Flow[Father[X]][X]+=CurrentF;
                        Flow[X][Father[X]]-=CurrentF;
                    }
                    F+=CurrentF;
                }
            }
        }
    }
    Type ();
    return 0;
}*/

#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

#define Infinit 2000000000
#define NMax 1005

using namespace std;

vector <int> G[NMax];
int N, Flow[NMax][NMax], Father[NMax], MaxFlow, Cap[NMax][NMax];
bool Viz[NMax];
queue <int> Queue;


void Read ()
{
    ifstream fin ("maxflow.in");
    int M, X, Y, Z;
    fin >> N >> M;
    for (; M>0; --M)
    {
        fin >> X >> Y >> Z;
        G[X].push_back (Y);
        G[Y].push_back (X);
        Cap[X][Y]+=Z;
    }
    fin.close ();
}

void Type ()
{
    ofstream fout ("maxflow.out");
    fout << MaxFlow << "\n";
    fout.close ();
}

bool BFS (int Start, int End)
{
    for (int i=1; i<=N; ++i)
    {
        Viz[i]=false;
    }
    Viz[Start]=true;
    Queue.push (Start);
    while (!Queue.empty ())
    {
        int X=Queue.front ();
        Queue.pop ();
        if (X==End)
        {
            continue;
        }
        for (unsigned i=0; i<G[X].size (); ++i)
        {
            int V=G[X][i];
            if ((!Viz[V]) and (Cap[X][V]-Flow[X][V]>0))
            {
                Queue.push (V);
                Father[V]=X;
                Viz[V]=true;
            }
        }
    }
    if (Viz[End])
    {
        return true;
    }
    return false;
}

inline int Min (int a, int b)
{
    if (a<b)
    {
        return a;
    }
    return b;
}

int main()
{
    int F=0;
    Read ();
    for (MaxFlow=0; BFS (1, N); MaxFlow+=F)
    {
        F=0;
        for (unsigned i=0; i<G[N].size (); ++i)
        {
            int X=G[N][i];
            if ((Viz[X]) and (Cap[X][N]-Flow[X][N]>0))
            {
                Father[N]=X;
                int CurrentF=Infinit;
                for (X=N; X>1; X=Father[X])
                {
                    CurrentF=Min (Cap[Father[X]][X]-Flow[Father[X]][X], CurrentF);
                }
                if (CurrentF>0)
                {
                    for (X=N; X>1; X=Father[X])
                    {
                        Flow[Father[X]][X]+=CurrentF;
                        Flow[X][Father[X]]-=CurrentF;
                    }
                    F+=CurrentF;
                }
            }
        }
    }
    Type ();
    return 0;
}