Cod sursa(job #2264969)

Utilizator slym777Darii Dan slym777 Data 20 octombrie 2018 14:01:46
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 kb

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

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

int n,m,x,y,c,maxflow = 0;
int GR[1010][1010];
int tata[1010], viz[1010];

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())
    {
        x = Q.front();
        Q.pop();
        for (int j = 1; j <= n; j++)
            if (GR[x][j] > 0 && !viz[j])
            {
                Q.push(j);
                tata[j] = x;
                viz[j] = 1;
                if (j == n)
                    return true;
            }
    }
    return false;
}

int main()
{
    fin >> n >> m;
    for (int k = 0; k < m; k++)
    {
        fin >> x >> y >> c;
        GR[x][y] = c;
    }
    /*for (int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
            cout << GR[i][j] << " ";
        cout << "\n";
    }*/

    while (BFS())
    {
      /*  for (int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
                cout << GR[i][j] << " ";
            cout << "\n";
            }
        cout << "\n";*/
        x = n;
        y = INT_MAX;
        while (tata[x] != 0)
        {
            y = min(GR[tata[x]][x],y);
            x = tata[x];
        }
        x = n;
        while (tata[x] != 0)
        {
            GR[tata[x]][x] -= y;
            GR[x][tata[x]] += y;
            x = tata[x];
        }
    }
    for (int i = 1; i <= n; i++)
        maxflow += GR[i][1];
    fout << maxflow;
    return 0;
}