Cod sursa(job #2913397)

Utilizator YosifIosif Andrei Stefan Yosif Data 14 iulie 2022 11:09:43
Problema Flux maxim Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 6.07 kb
#include<bits/stdc++.h>
using namespace std;

class input {
private:
    FILE* fin;
    char* t;
    int sp;
    const int sz = 10000;

    char read()
    {
        if (sp == sz)
        {
            fread(t, 1, sz, fin);
            sp = 0;
            return t[sp++];
        }
        else
            return t[sp++];
    }
public:
    input(const char* name)
    {
        fin = fopen(name, "r");
        sp = sz;
        t = new char[sz]();
    }

    void close()
    {
        fclose(fin);
    }

    void open(const char* name)
    {
        fin = fopen(name, "r");
        sp = sz;
        t = new char[sz]();
    }

    input& operator >> (int& n)
    {
        char c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;

        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (c != '\0' && isdigit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    input& operator >> (char& s)
    {
        char c = read();

        while (c != '\0' && c == '\n')
            c = read();

        s = c;

        return *this;
    }

    input& operator >> (long long& n)
    {
        char c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;

        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (c != '\0' && isdigit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    void getline(string& s)
    {
        char c = read();
        s = "";

        while (c != '\0' && c != '\n')
            s += c, c = read();
    }

    input& operator >> (string& s)
    {
        char c;
        c = read();
        s = "";

        while (c == '\n' || c == ' ')
            c = read();

        while (c != '\n' && c != '\0' && c != ' ')
            s += c, c = read();

        return *this;
    }

    input& operator >> (char* s)
    {
        char c;
        c = read();
        int i = 0;

        while (c == '\n' || c == ' ')
            c = read();

        while (c != '\n' && c != '\0' && c != ' ')
            s[i++] = c, c = read();

        return *this;
    }
};
class output {
private:
    FILE* fout;
    char* t;
    int sp;
    const int sz = 10000;

    void write(char c)
    {
        if (sp == sz)
        {
            fwrite(t, 1, sz, fout);
            sp = 0;
            t[sp++] = c;
        }
        else
            t[sp++] = c;
    }

public:
    output(const char* name)
    {
        fout = fopen(name, "w");
        sp = 0;
        t = new char[sz]();
    }

    ~output()
    {
        fwrite(t, 1, sp, fout);
    }

    output& operator << (int n)
    {
        if (n < 0)
        {
            write('-');
            n *= -1;
        }
        if (n <= 9)
            write(char(n + '0'));
        else
        {
            (*this) << (n / 10);
            write(char(n % 10 + '0'));
        }

        return *this;
    }

    output& operator << (char c)
    {
        write(c);

        return *this;
    }

    output& operator << (const char* s)
    {
        int i = 0;

        while (s[i] != '\0')
            write(s[i++]);

        return *this;
    }

    output& operator << (long long n)
    {
        if (n < 0)
        {
            write('-');
            n *= -1;
        }
        if (n < 10)
            write(char(n + '0'));
        else
        {
            (*this) << (n / 10);
            write(char(n % 10 + '0'));
        }

        return *this;
    }

    output& operator << (string s)
    {
        for (auto i : s)
            write(i);

        return *this;
    }

    void precizion(double x, int nr)
    {
        int p = floor(x);

        *this << p;

        if (nr == 0)
            return;

        write('.');

        for (int i = 1; i <= nr; i++)
        {
            x -= floor(x);
            x *= 10;

            write(int(x) + '0');
        }
    }
};

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

int n, m, in[1001], out[1001], maxi[1001], flux[1001];
vector <pair<int, int>> g[1001];
vector <pair<int, int>> h[1001];

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

    int i, j, k;

    for(int l = 1; l <= m; l++)
    {
        fin >> i >> j >> k;
        g[i].push_back(make_pair(j, k));
        h[j].push_back(make_pair(i, k));
        in[j]++;
        out[i]++;
    }

    queue <int> Q;

    for (i = 1; i <= n; i++)
        if (out[i] == 0)
            Q.push(i);

    maxi[n] = INT_MAX;

    while (!Q.empty())
    {
        int nod = Q.front();
        Q.pop();

        for (auto i : h[nod])
            if (out[i.first] == 1)
            {
                maxi[i.first] += min(maxi[nod], i.second);
                out[i.first] = 0;
                Q.push(i.first);
            }
            else
            {
                maxi[i.first] += min(maxi[nod], i.second);
                out[i.first]--;
            }
    }

    if (maxi[1] == 0)
    {
        fout << "Apa nu ajunge!";
        return 0;
    }

    for (i = 1; i <= n; i++)
        if (in[i] == 0)
            Q.push(i);

    flux[1] = INT_MAX;

    while (!Q.empty())
    {
        int nod = Q.front();
        Q.pop();

        for(auto i : g[nod])
            if (in[i.first] == 1)
            {
                in[i.first] = 0;
                int aux = min(flux[nod], min(i.second, maxi[i.first]));
                flux[i.first] += aux;
                flux[nod] -= aux;
                Q.push(i.first);
                maxi[i.first] -= aux;
            }
            else
            {
                in[i.first]--;
                int aux = min(flux[nod], min(i.second, maxi[i.first]));
                flux[i.first] += aux;
                flux[nod] -= aux;
                maxi[i.first] -= aux;
            }
    }

    fout << flux[n];

    return 0;
}