Cod sursa(job #2904963)

Utilizator YosifIosif Andrei Stefan Yosif Data 18 mai 2022 22:27:50
Problema Flux maxim de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 5.21 kb
#include<bits/stdc++.h>
using namespace std;

class input {
private:
    FILE* fin;
    char* t;
    int sp;

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

    void close()
    {
        fclose(fin);
    }

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

    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();
    }
};
class output{
private:
    FILE* fout;
    char* t;
    int sp;

    void write(char c)
    {
        if (sp == 5000)
        {
            fwrite(t, 1, 5000, fout);
            sp = 0;
            t[sp++] = c;
        }
        else
            t[sp++] = c;
    }
    
public:
    output(const char* name)
    {
        fout = fopen(name, "w");
        sp = 0;
        t = new char[5000]();
    }
    ~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');
        }
    }
};

input fin("fmcm.in");
output fout("fmcm.out");
const int N = 360;
const int inf = 100000000;

int flow[N][N], w[N], cost[N][N], d[N], t[N], n, m, cap[N][N];
vector<vector<int>> g(N);

int bfs(int start, int stop)
{
    queue<int> Q;
    Q.push(start);
    
    for (int i = 1; i <= n; i++)
        d[i] = inf, w[i] = 0;

    w[start] = inf;
    d[start] = 0;

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

        for (auto i : g[nod])
        {
            if (d[i] > d[nod] + cost[nod][i] && cap[nod][i] - flow[nod][i] > 0)
            {
                d[i] = d[nod] + cost[nod][i];
                t[i] = nod;
                w[i] = min(w[nod], cap[nod][i] - flow[nod][i]);

                if (i != stop)
                    Q.push(i);
            }
        }
    }

    if (d[stop] == 0)
        return 0;
    else
        return w[stop];
}

int maxFlow(int start, int stop)
{
    int r = 0, f = 0;

    while (true)
    {
        int fl = bfs(start, stop);

        if (fl == 0)
            break;

        int a = NULL, b = stop;
        f += fl;

        while (b != start)
        {
            a = t[b];
            flow[a][b] += fl;
            r += cost[a][b];
            cost[a][b] = 0;
            b = a;
        }
    }

    return r;
}

int main()
{
    int start, stop;

    fin >> n >> m >> start >> stop;

    int i, j, capacity, c;

    for (int k = 1; k <= m; k++)
    {
        fin >> i >> j >> capacity >> c;

        g[i].push_back(j);
        cap[i][j] = capacity;
        cost[i][j] = c;
    }

    fout << maxFlow(start, stop);

    return 0;
}