Cod sursa(job #1516975)

Utilizator DrumeaVDrumea Vasile DrumeaV Data 3 noiembrie 2015 19:14:35
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.75 kb
#include <stdio.h>
#include <string.h>
#include <vector>
#include <deque>
#include <algorithm>

using namespace std;

const int MN = 1003;
const int IF = 0x3f3f3f3f;

int N,M,Sol;
int Pt[MN];
int Cp[MN][MN],Fl[MN][MN];
bool B[MN];
vector< int > G[MN];
deque< int > D;

bool BFS()
{
    memset(B,0,sizeof(B));
    D.clear();
    B[1] = 1;
    D.push_back(1);

    while (!D.empty())
    {
        int X = D.front(),Sz = G[X].size();
        D.pop_front();

        if (X == N)
           continue;

        for (int j = 0;j < Sz;j++)
        {
            int Y = G[X][j];

            if (B[Y] || Cp[X][Y] == Fl[X][Y])
               continue;

            B[Y] = 1;
            D.push_back(Y);
            Pt[Y] = X;
        }
    }

    return B[N];
}

int main()
{
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);

     scanf("%d %d",&N,&M);

     for (int i = 1;i <= M;i++)
     {
         int X,Y,Z;
         scanf("%d %d %d",&X,&Y,&Z);
         Cp[X][Y] += Z;
         G[X].push_back(Y);
         G[Y].push_back(X);
     }

     while (BFS())
     {
         int Sz = G[N].size();

         for (int i = 0;i < Sz;i++)
         {
             int X = G[N][i],aux = IF;

             if (!B[X] || Cp[X][N] == Fl[X][N])
                continue;

             Pt[N] = X;

             for (X = N;X != 1;X = Pt[X])
                 aux = min(aux,Cp[Pt[X]][X] - Fl[Pt[X]][X]);

             if (!aux)
                continue;

             for (X = N;X != 1;X = Pt[X])
             {
                 Fl[Pt[X]][X] += aux;
                 Fl[X][Pt[X]] -= aux;
             }

             Sol += aux;
         }
     }

   printf("%d\n",Sol);

  return 0;
}