Cod sursa(job #1193751)

Utilizator lucian666Vasilut Lucian lucian666 Data 1 iunie 2014 17:24:27
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.56 kb



#include<fstream>
#include<vector>
#include<queue>
#include<cstring>

#define NN 1009
#define pb push_back

using namespace std;
ofstream out("maxflow.out");


int cap[NN][NN] , flux[NN][NN] , uz[NN] , n , m , flow , father[NN] , minn;

vector<int>G[NN];
typedef vector<int>::iterator IT;


void read();
bool bf();
void solve();

int main()
{

        read();
        solve();

        out << flow << '\n';

        return 0;
}


void read()
{

    ifstream in("maxflow.in");

    in >> n >> m;
    for( int x , y ,c , i =1 ; i<=m ; i++)
    {

        in >> x >> y >> c;
        G[x].pb(y);
        G[y].pb(x);

        cap[x][y] = c;

    }
}


bool bf()
{

    memset(uz,0,sizeof(uz));
    uz[1] = 1;
    queue<int>Q;

    Q.push(1);

    int k ;
    while(!Q.empty())
    {

        int k = Q.front();
        Q.pop();
        if(k!=n)
            for( IT i = G[k].begin(); i != G[k].end() ; ++i )
                    if( !uz[*i] && cap[k][*i] > flux[k][*i] )
            {

                uz[*i] = 1;
                father[*i] = k;
                Q.push(*i);
            }
    }

    return uz[n];
}

void solve()
{

    for(; bf() ; flow+=minn )
    {

        minn = 0x3f3f3f3f;

        for( int x = n ; x !=1 ; x = father[x] )
            minn = min ( minn , cap[ father[x] ][ x ] - flux[ father[x] ][ x ] );

        for( int x = n ; x !=1 ; x = father[x] )
        {

            flux[ father[x] ][ x ] +=minn;
            flux[ x ][ father[x] ] -=minn;
        }
    }
}