Cod sursa(job #2207253)

Utilizator slym777Darii Dan slym777 Data 25 mai 2018 12:29:27
Problema Flux maxim Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.76 kb
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int GR[100][100];
int tata[100]={0}, viz[100];
int n,m;

bool BFS()
{
    for (int i = 1 ; i <=n; i++)
        tata[i] = viz[i] = 0;
    queue <int> Q;
    Q.push(1);
    tata[1] = 0; viz[1] = 1;
    while (!Q.empty())
    {
        int x = Q.front();
        Q.pop();
        for ( int j=1 ; j <= n; j++)
            if (GR[x][j] > 0 && !viz[j])
        {
            tata[j] = x;
            viz[j] = 1;
             Q.push(j);
            if (j == n)
            {
                 // cout << "\tAM ajuns aici" << endl;
                return true;
            }
        }
    }
    return false;

}

int main()
{
    int flux_max = 0,k,i,j,c;
    ifstream fin("maxflow.in");
    ofstream fout("maxflow.out");
    fin >> n >> m;
    for (k = 0; k < m; k++)
    {
        fin >> i >> j >> c;
        GR[i][j] = c;
        GR[j][i] = 0;
    }
    while (BFS())
    {
        int minim = 100; /// putem tine minte cel mai mare c
        int x = n;
        while ( tata[x] != 0)
        {
            if (minim > GR[tata[x]][x])
            {
                minim = GR[tata[x]][x];
            }
            x = tata[x];
        }
        //cout << "minim = " << minim;
        x = n;
        while (tata[x] != 0)
        {
            GR[x][tata[x]] += minim;
            GR[tata[x]][x] -= minim;
            x = tata[x];
            //cout << "!";
        }
    }
    //cout << " ? ";
    cout << endl;
    for (int j = 1; j <= n; j++ )
    {
        //cout << GR[j][1] << endl;
        if (GR[j][1] > 0)
            flux_max += GR[j][1]; /// calc flux maxim
    }
    fout << flux_max << endl;
    fin.close();
    fout.close();
    return 0;
}