Cod sursa(job #2568709)

Utilizator Fatu_SamuelFatu Samuel Fatu_Samuel Data 4 martie 2020 09:34:06
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 1005;
const int inf = (1<<30);

int n, m, ft;

int ca[nmax][nmax], fl[nmax][nmax], p[nmax];

vector < int > l[nmax];



int main()
{
    fin >> n >> m;

    for (int i = 1; i <= m; i++)
    {
        int x, y, c;

        fin >> x >> y >> c;

        l[x].push_back(y);
        l[y].push_back(x);

        ca[x][y] += c;
    }

    do
    {
        for (int i = 1; i <= n; i++)
            p[i] = inf;

        queue < int > q;

        q.push(1);

        while (!q.empty())
        {
            int x = q.front();
            q.pop();

            for (int el : l[x])
            {
                if (p[el] == inf && fl[x][el] < ca[x][el])
                {
                    p[el] = x;
                    q.push(el);
                }
            }
        }

        if (p[n] != inf)
        {
            int fm = inf, x = n;

            while(x != 1)
            {
                fm = min(fm, ca[p[x]][x] - fl[p[x]][x]);
                x = p[x];
            }

            x = n;

            while (x != 1)
            {
                fl[p[x]][x] += fm;
                fl[x][p[x]] -= fm;
                x = p[x];
            }

            ft += fm;
        }
    }
    while (p[n] != inf);

    fout << ft;

    fin.close();
    fout.close();
    return 0;
}