Cod sursa(job #2243236)

Utilizator CozmaCatalinCozma Catalin CozmaCatalin Data 20 septembrie 2018 10:12:02
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.76 kb
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

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

using namespace std;

const int LIM = 1024;

vector < int > myVector[LIM+1];
queue < int > myQueue;

int theFlux[LIM+1][LIM+1];
int theAmount[LIM+1][LIM+1];
bool beenThere[LIM+1];
int theRoad[LIM+1];
int N,M,theAnswer;

bool BFS()
{
    memset(beenThere,false,sizeof(beenThere));
    beenThere[1] = true;
    myQueue.push(1);

    while(!myQueue.empty())
    {
        int node = myQueue.front();
        myQueue.pop();
        if(node == N) continue;

        for ( auto V : myVector[node])
        {
            if(theAmount[node][V] > theFlux[node][V] && !beenThere[V])
            {
                beenThere[V] = true;
                myQueue.push(V);
                theRoad[V] = node;
            }
        }
    }
    return beenThere[N];
}


int main()
{
   in >> N >> M;
   for ( int i = 1; i <= M ; ++i)
   {
       int x,y,z;
       in >> x >> y >> z;
       theAmount[x][y]=z;
       myVector[x].push_back(y);
       myVector[y].push_back(x);
   }

   while(BFS())
   {
     for ( int i = 0 ; i < myVector[N].size() ; i++)
     {
         int V = myVector[N][i];
         if(theAmount[V][N] > theFlux[V][N] && beenThere[V])
         {
             theRoad[N] = V;
             int fMin = INF;

             for ( int k = N ; k!= 1 ; k = theRoad[k])
                fMin = min(fMin , theAmount[theRoad[k]][k] - theFlux[theRoad[k]][k]);
             for( int k = N ; k != 1 ; k = theRoad[k])
             {
                 theFlux[theRoad[k]][k] += fMin;
                 theFlux[k][theRoad[k]] -= fMin;
             }
             theAnswer+=fMin;
         }
     }
   }
   out << theAnswer;
}