Cod sursa(job #2906466)

Utilizator sandrapioanaSandra Pirvanescu sandrapioana Data 26 mai 2022 09:08:07
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.57 kb
#include <bits/stdc++.h>
using namespace std;

// numarul maxim de noduri
#define NMAX 1005

class Task {
public:
    void solve() {
        read_input();
        print_output(get_result());
    }

private:
    // n = numar de noduri, m = numar de muchii
    int n, m;

    // adj[i] = lista de adiacenta a nodului i
    vector<int> adj[NMAX];

    // cap[i][j] = capacitatea arcului i -> j
    int cap[NMAX][NMAX];
    int f[NMAX][NMAX];

    void read_input() {
        ifstream fin("in");
        fin >> n >> m;
        memset(cap, 0, sizeof cap);
        for (int i = 1, x, y, c; i <= m; i++) {
            // x -> y de capacitate c
            fin >> x >> y >> c;
            adj[x].push_back(y);
            adj[y].push_back(x);

            // Presupunem existenta mai multor arce x -> y cu capacitati c1, c2, ...
            // Comprimam intr-un singur arc x -> y cu capacitate
            // cap[x][y] = c1 + c2 + ...
            cap[x][y] += c;
        }
        fin.close();
    }
    bool bfs(int rGraph[V][V], int s, int t, int parent[])
    {
        // Create a visited array and mark all vertices as not visited
        bool visited[V];
        memset(visited, 0, sizeof(visited));
        // Create a queue, enqueue source vertex and mark source vertex
        // as visited
        queue <int> q;
        q.push(s);
        visited[s] = true;
        parent[s] = -1;
        // Standard BFS Loop
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            for (int v = 0; v < V; v++)
                if (visited[v] == false && rGraph[u][v] > 0)
                {
                    q.push(v);
                    parent[v] = u;
                    visited[v] = true;
                }
        }
        // If we reached sink in BFS starting from source, then return
        // true, else false
        return visited[t] == true;
    }  
    int get_result() {
        //
        // TODO: Calculati fluxul maxim pe graful orientat dat.
        // Sursa este nodul 1.
        // Destinatia este nodul n.
        //
        // In adj este stocat graful neorientat obtinut dupa ce se elimina orientarea
        // arcelor, iar in cap sunt stocate capacitatile arcelor.
        // De exemplu, un arc (x, y) de capacitate c va fi tinut astfel:
        // cap[x][y] = c, adj[x] contine y, adj[y] contine x.
        //
        int max_flow = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                f[i][j] = 0;
            }
        }
        while(true) {
            int flow = bfs(0, n);
            if (flow == 0) {
                break;
            }
            maxFlow += flow;
            int currNode = n;
            while(currNode != 0) {
                int prevNode = parList[currNode];
                flowPassed[prevNode][currNode] += flow;
                flowPassed[currNode][prevNode] -= flow;
                currNode = prevNode;
            }
        }


        return max_flow;
    }

    void print_output(int result) {
        ofstream fout("out");
        fout << result << '\n';
        fout.close();
    }
};

// [ATENTIE] NU modifica functia main!
int main() {
    // * se aloca un obiect Task pe heap
    // (se presupune ca e prea mare pentru a fi alocat pe stiva)
    // * se apeleaza metoda solve()
    // (citire, rezolvare, printare)
    // * se distruge obiectul si se elibereaza memoria
    auto* task = new (nothrow) Task(); // hint: cppreference/nothrow
    if (!task) {
        cerr << "new failed: WTF are you doing? Throw your PC!\n";
        return -1;
    }
    task->solve();
    delete task;
    return 0;
}