Cod sursa(job #1460974)

Utilizator petru.cehanCehan Petru petru.cehan Data 14 iulie 2015 14:45:00
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.61 kb
//Algoritmul lui Ford Fulkerson
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

int N , M ;

struct nod
{
    nod * next ;
    int info ;
    int capacity ;
    int flow ;
} ;

nod * L [ 1001 ] , * LR [ 1001 ] ; // lista de vecini respectiv lista reziduala

void Add ( int a , int b , int c ) ;
void Citire ()
{
    fin >> N >> M ;
    int a , b , c ;
    while ( M >= 1 )
    {
        fin >> a >> b >> c ;
        Add ( a , b , c ) ;
        -- M ;
    }
}

void Add ( int a , int b , int c )
{
    nod * p = new nod ;
    p->info = b ;
    p->capacity = c ;
    p->flow = 0 ;
    p->next = L [a] ;
    L [a] = p ;

    p = new nod ;
    p->info = b ;
    p->capacity = c ;
    p->flow = -1 ; // oricum la Graful Rezidual nu conteaza
    p->next = LR [a] ;
    LR [a] = p ;
}

int viz [ 1001 ] , viz2 [ 1001] ;
int dest ; // destinatie
int minim ; // capacitate reziduala minima
int fluxmax ; // fluxul maxim cerut
bool sw = 1 ; // cat timp mai exista augmeting path

void DFSFindAugmentingPath ( nod * sursa )
{
    if ( minim > sursa->capacity )
            minim = sursa->capacity ;

    viz [ sursa->info ] = 1 ;

    for ( nod * p = LR [ sursa->info ] ; p != 0 ; p = p->next )
       {
           if ( viz [ p->info ] == 0 )
                    DFSFindAugmentingPath ( p ) ;

           if ( p->info == dest )
            {
                sw = 1 ;
                fluxmax += minim ;
                p->capacity -= minim ;
                L [ sursa->info ] ->flow += minim ;
                return ;
            }

           LR [ sursa -> info ] ->capacity -= minim ;
           L [ sursa->info ]->flow += minim ;

           nod * pred = 0 ;
           for ( nod * y = LR [ sursa->info ] ; y != 0 && y->capacity == 0 ; pred = y , y = y->next )
             {
                if ( y ->capacity == 0 )
                    {
                        if ( pred == 0 )
                            LR [ sursa->info ] = y->next ;
                        else
                            pred->next = y->next ;

                        delete y ;

                    }

                return ;
             }

            return ;
       }

}

int main()
{
    Citire () ;
    dest = N ;
    Add ( 0 , 1 , 100000 ) ;

   while ( sw == 1 )
    {
        sw = 0 ;
        minim = 100001 ;
        DFSFindAugmentingPath ( LR [0] ) ;
        for ( int i = 0 ; i <= 1001 ; ++ i )
                        viz [i] = 0 ;
    }

    fout << fluxmax ;
    return 0;
}